0

i have a problem with merged stream and StreamBuilder. im trying to merge multiple streams from firestore that each one represents a grocery list. my result should be a ListView that combines all list in a some group. from some reason,my StreamBuilder shows a single stream list in one tab but doesnt show it in another.

group list app photo personal list app photo

code:

  Stream<QuerySnapshot<Map<String, dynamic>>> getPesonalStream<T>() {
final userGroceries =
    _fireStore.collection("users").doc(email).collection("groceries");
return userGroceries.snapshots();


}

  Stream<QuerySnapshot<Map<String, dynamic>>> getGroupStream<T>() {
    List<Stream<QuerySnapshot<Map<String, dynamic>>>> list = [];
_fireStore
    .collection("groups")
    .doc(gid)
    .snapshots()
    .forEach(((docSnapshot) {
  List<dynamic> members = docSnapshot.data()!["members"];

  list = members
      .map((member) => _fireStore
          .collection("users")
          .doc(member)
          .collection("groceries")
          .snapshots())
      .toList();
}));
return CombineLatestStream(list,
        (values) => values.last as QuerySnapshot<Map<String, dynamic>>)
    .asBroadcastStream();
// return StreamGroup.merge(list).asBroadcastStream();


}

as you can see iv tried a few ways to combine my streams and non workes

body:

  body: TabBarView(children: [
        _buildContent(
          context,
          db.getGroupStream(),
        ),
        _buildContent(
          context,
          db.getPesonalStream(),
        ),
      ]),

my builder:

 Widget _buildContent(BuildContext context, Stream<QuerySnapshot> stream) {
return StreamBuilder<QuerySnapshot>(
  stream: stream,
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      final docs = snapshot.data!.docs;
      if (docs.isEmpty) {
        return Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "The list is empty",
              style: TextStyle(fontSize: 32, color: Colors.black54),
            ),
            Text(
              "Add a new item to get started",
              style: TextStyle(fontSize: 16, color: Colors.black54),
            ),
          ],
        ));
      }
      int index = -1;
      final cards = docs
          .map((doc) => CrossableListTile(
              doc, _showSetGroceryButtomSheetForm, index++))
          .toList();
      return Container(
        padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
        child: ListView(
          children: cards,
        ),
      );
    } else if (snapshot.hasError) {
      return Center(
        child: Column(
          children: [Text("An error has occured while loading you'r list")],
        ),
      );
    }
    return Center(
        child: CircularProgressIndicator(
      color: Colors.black,
    ));
  },
);

}

  • yes but i also tried StreamGroup.merge(list).asBroadcastStream() and it doesnt work so i dont think this is the problem – Harel Moshayof Sep 15 '21 at 10:43
  • i think you are missing the point, as i said iv used StreamGroup.merge(list).asBroadcastStream() first and it should have worked fine,but there is still a problem. not only that,but if im using values.last i should get something in my listView but im not getting any data from the snapshot. – Harel Moshayof Sep 15 '21 at 12:21
  • i have 3 broadcast streams . each stream is the grocery list stream from firebase for a specific user – Harel Moshayof Sep 18 '21 at 08:04
  • Yes, exactly. As i saied, i dont think this is the problem – Harel Moshayof Sep 19 '21 at 10:36

0 Answers0