0

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 ;-)

Brooklyn Lee
  • 369
  • 2
  • 6
  • 15
  • I've tried using .getDocuments().then((item) => item.documents.length) but it returns future, which is hard for me to use them in feed_page since it uses streamProvider – Brooklyn Lee Jul 02 '20 at 16:36

1 Answers1

0

Did you try FutureBuilder approach ?


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
            body: FutureBuilder<List<String>>(
          future: fetchFunction(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return CircularProgressIndicator();
            else if (snapshot.connectionState == ConnectionState.done) {
              return snapshot.data.isEmpty?Text("empty list"):ListView(children: [...snapshot.data.map.((post)=>Text(post)),],);
            }
          },
        ),
      ),
    );
  }
}
Kadri Nadir
  • 349
  • 2
  • 9
  • Thanks. it seems like I should use Future instead of Stream. But I want my app to keep listening with Stream. Is there a way to convert FutureBuilder to StreamBuilder? – Brooklyn Lee Jul 03 '20 at 02:46
  • 1
    i never used Streambuilder so in practice i cant give you help , but yes you can use FutureBuilder and providers in a bloc-pattern architecture fits very well with conception of flutter in my opinion . – Kadri Nadir Jul 03 '20 at 10:56
  • i did a litle introduction on this post : https://stackoverflow.com/questions/62718242/clarification-about-provider-in-flutter-how-to-save-data/62718721#62718721 – Kadri Nadir Jul 04 '20 at 08:29