[cloud_firestore] DocumentSnapshot promotion or null safe data
#6060
Answered
by
rrousselGit
stargazing-dino
asked this question in
Q&A
|
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!) |
Answered by
rrousselGit
May 6, 2021
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;
}
} |
1 reply
Answer selected by
stargazing-dino
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should be able to implement that with an extension: