Hi I am now trying to get Stream<List<Post>>
with fetchAllPosts
function.
toPosts
is a Transformer that turns QuerySnapshot into a Stream<List>.
// firebase_provider.dart
Stream<List<Post>> fetchAllPosts() {
return _firestore
.collection('Posts')
.orderBy('commenttime', descending: true)
.snapshots()
.transform(toPosts);
}
This Stream<List> value will be used as a ListView within the StreamProvider in feed_page.dart
// feed_page.dart
Widget build(BuildContext context) {
return StreamProvider<List<Post>>.value(
value: firestoreProvider.fetchAllPosts()
However, if the snapshot is empty, I want to handle it differently in feed_page.dart(not in ListView)
How should I handle this? The following method is what I've tried, but I'm not sure what to put in the last else statement in this case. Or I think there will be a better way
Stream<List<Post>> fetchAllPosts2() {
if (_firestore
.collection(COLLECTION_POSTS)
.orderBy(KEY_POSTTIME, descending: true)
.snapshots()
.length
.toString() !=
"0") {
return _firestore
.collection(COLLECTION_POSTS)
.orderBy(KEY_POSTTIME, descending: true)
.snapshots()
.transform(toPosts);
} else {
return ???????????;
}
}
Help me plz ;-)