Satish Bandaru
Satish Bandaru

Reputation: 276

Flutter firestore streambuilder with a future

I have a simple question. The reference to my firestore collection is dynamic. In this piece of code, getDocumentReference() gives me a reference to document after checking the user's email. I use this document reference to get my snapshots.

Future<Stream<QuerySnapshot>> getHabits() async {
    DocumentReference document = await getDocumentReference();
    var snapshots =  document.collection('habits').snapshots();
    return snapshots;
}

As you can see, I want to use this Future<Stream<QuerySnapshot>> for a streambuilder. How can I do that? I tried something like this. But it is not taking the future as input to stream

return StreamBuilder(
   stream: getHabits(),
);

Upvotes: 2

Views: 349

Answers (1)

Rustem Kakimov
Rustem Kakimov

Reputation: 2671

You can wrap it in a FutureBuilder:

return FutureBuilder<Stream<QuerySnapshot>>(
   future: getHabits(),
   builder: (context, snapshot) {
      if (snapshot.hasData) {
          return StreamBuilder(stream: snapshot.data); // Success
      } else if (snapshot.hasError) {
          return Text('${snapshot.error}'); // Error
      } else {
          return CircularProgressIndicator(); // Loading
      }
   },

);

Upvotes: 2

Related Questions