How to use StreamZip with StreamBuilder?

3.1k Views Asked by At

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?

3

There are 3 best solutions below

3
On

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

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

Package dart:async

4
On
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

0
On
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) { ... }
);