[cloud_firestore] DocumentSnapshot promotion or null safe data
#6060
-
Hi ! Currently, a So like turning this @override
T? data() {
if (!_originalDocumentSnapshot.exists) return null;
return _fromFirestore(_originalDocumentSnapshot, null);
}
@override
bool get exists => _originalDocumentSnapshot.exists; into something like @override
T data() {
if (!_originalDocumentSnapshot.exists) {
// Check if it's nullable
if (null is T) {
return null;
} else {
throw 'Non-nullable type given but no snapshot does not exist.';
}
}
return _fromFirestore(_originalDocumentSnapshot, null);
}
@override
bool get exists {
if (null is T) {
return _originalDocumentSnapshot.exists;
} else {
return true;
}
} I ran into this because I was hoping to somehow promote If this is not possible could there be a separate method to get cc @rrousselGit (Sorry for pinging!) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You should be able to implement that with an extension: extension<T> on DocumentSnapshot<T> {
T get requiredData {
final value = data();
if (!value.exists) throw Error('not found');
return value as T;
}
} |
Beta Was this translation helpful? Give feedback.
You should be able to implement that with an extension: