DEV Community

Cover image for [Looking for advice] Combining streams in Flutter
Sandor Dargo
Sandor Dargo

Posted on

[Looking for advice] Combining streams in Flutter

Hello everyone!

I'm looking for some help, if you know Flutter and Firebase and have a bit of time, please share your advice.

What I'm building

I'm building an app where I can share my goals with a buddy on a daily/weekly basis. The number of daily goals is arbitrary and it's sent in a chat-like window. At the end of the day, the user can check-off each item.

The data behind

I want to use Firestore to store the data.

I have a collection to store the daily_goals, in that subcollections on a daily basis and for each day, I store each goal as a subcollection, in that I store the goal description as a string and its completion as a boolean.

Alt Text

The question finally!

In the Streambuilder, I'd like to combine subcollections of goals from multiple collections (dates).

But when I create my stream, it just fetches the collections representing the days:

FirebaseFirestore.instance
        .collection('daily_goals')
        .limit(10)
        .snapshots();
Enter fullscreen mode Exit fullscreen mode

If I want to retrieve the subcollections for each day later, then I cannot do that in a synchronous way and the data arrives from Firestore later than the widgets would be built...

When I just try to get (here just print) the data, I get it later then I'd need it:

var l = new List();
await document.reference.collection("goals").snapshots().forEach((element) {
      print(document.reference);
      element.docs.forEach((element) {
        print(element.data());
        l.add(element.data()["goal"]);
      });
});
Enter fullscreen mode Exit fullscreen mode

Is it even possible that I plan to do? Get the subcollections of multiple streams combined so that I can pass it to the streambuilder? Or I should rather change the data model I planned to use?

Thanks a lot for your advice!

Top comments (2)

Collapse
 
viveky259259 profile image
Vivek Kumar Yadav

hi, I would advice you to refer Firebase Collection Group Queries.
firebase.google.com/docs/firestore...

Collapse
 
sandordargo profile image
Sandor Dargo

Hi Vivek,
Thank you so much, this is exactly what I needed!