Search code examples
fluttergoogle-cloud-firestore

Firestore subcollections in Stream<QuerySnapshot>?


Not sure how to do this - I have a collection with agenda items and would like to do a subcollection where speakers for each agenda item would be held. But I'm not sure how to do this?

`class _PanelsGreeceDayOneState extends State<PanelsGreeceDayOne> {
  final Stream<QuerySnapshot> _greecePanelsOne = FirebaseFirestore.instance
      .collection('Greece')
      .where('day', isEqualTo: 'Thursday, 6th April')
      .orderBy('number', descending: false)
      .get()
      .then((QuerySnapshot querySnapshot) => querySnapshot.docs.forEach((doc) {
         FirebaseFirestore.instance
               .doc(doc.id)
               .collection("speakers")
               .get();
           
      }))
      .snapshots();`

This is what I've done but it's telling me that The method 'snapshots' isn't defined for the type 'Future'. Before I added the speakers subcollection it was working exactly as intended, but now that I need this added functionality I'm stumped :(


Solution

  • Found how to do this is anyone else is having issues, the trick is to do it through StreamBuilder instead, which then let's you call a collection within another collection, as shown below.

    Also this video is very helpful: https://www.youtube.com/watch?v=r6AkdYf_bkE&t=1s

     StreamBuilder(
                                                          stream: FirebaseFirestore
                                                              .instance
                                                              .collection("Posts")
                                                              .doc(widget.postId)
                                                              .collection(
                                                                  "comments")
                                                              .snapshots(),
                                                          builder:
                                                              (context, snapshot) {
                                                            if (snapshot.hasData) {
                                                              return 
    //WHATEVER YOU NEED TO HAVE RETURNED
                                                              );
                                                            } else if (snapshot
                                                                .hasError) {
                                                              return Center(
                                                                child: Text(
                                                                    'Error${snapshot.error}'),
                                                              );
                                                            } else {
                                                              return const Center(
                                                                child:
                                                                    CircularProgressIndicator(),
                                                              );
                                                            }
                                                          },
                                                        ),