How to use StreamZip with StreamBuilder?

985
Stream<List<QuerySnapshot>> combineStream() {

  return StreamZip([stream1, stream2]);

}


return StreamBuilder(
  stream: combineStream(),
  builder: (context, snapshot) {

    List<DocumentSnapshot> documentSnapshot = [];

    List<dynamic> querySnapshot = snapshot.data.toList();

    querySnapshot.forEach((query) {
      documentSnapshot.addAll(query.docs);
    })

  }
);

Your documentSnapshot now contains your combined streams

Share:
985
FlutterFirebase
Author by

FlutterFirebase

Updated on December 26, 2022

Comments

  • FlutterFirebase
    FlutterFirebase over 1 year

    I have two stream

    stream1
    stream2
    

    I can give one to StreamBuilder and it work. For example:

    return StreamBuilder(
      stream: stream1,
    

    But when I combine with StreamZip it now give error:

    StreamZip combinedStream() {
    return StreamZip(stream1, stream2]);
    
    }
    
    return StreamBuilder(
      stream: combinedStream,
    

    How I can combine stream1 and stream2 and give to StreamBuilder?

  • FlutterFirebase
    FlutterFirebase over 3 years
    Thanks for reply! I try this but it not update correct in UI. It only show one stream result. Maybe because it emit at wrong time?
  • FlutterFirebase
    FlutterFirebase over 3 years
    Thanks for reply! Is not simpler way with StreamZip?
  • Chichebe
    Chichebe over 3 years
    I'm making use of the StreamZip in the combineStream to zip the two streams together.
  • FlutterFirebase
    FlutterFirebase over 3 years
    Thanks for reply! Why cannot just use StreamZip alone?
  • Chichebe
    Chichebe over 3 years
    I've not tried it that way. The code snippet I posted is how I'm currently using it in my project, and it works for me.
  • Sergio
    Sergio over 3 years
    It should emit from both streams. Maybe another stream doesn't emit due to some condition?
  • Kamlesh
    Kamlesh almost 3 years
    Kindly share steam1 and steam2 data so that we can know how are you creating stream1 and stream2 variables and what type of data passing to merge function. Thanks. Look forward to hear you.