3

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
  • 2,163
  • 6
  • 28
  • 60

3 Answers3

2
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

Chichebe
  • 580
  • 5
  • 12
0

You can use StreamGroup.merge to merge two Streams into a single Stream:

StreamBuilder(
  stream: StreamGroup.merge(stream1, stream2),

Package dart:async

Sergio
  • 27,326
  • 8
  • 128
  • 149
  • 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 Dec 03 '20 at 20:52
  • It should emit from both streams. Maybe another stream doesn't emit due to some condition? – Sergio Dec 04 '20 at 07:23
  • 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. – Kamlesh Jun 16 '21 at 08:46
0
import 'package:rxdart/rxdart.dart';
import 'package:tuple/tuple.dart';


final Stream<A> stream1;
final Stream<B> stream2;
final Stream<Tuple2<A, B>> stream = Rx.zip2(
    stream1,
    stream2,
    (A a, B b) => Tuple2(a, b),
);

StreamBuilder<Tuple2<A, B>>(
    stream: stream,
    builder: (ctx, snapshot) { ... }
);