Search code examples
dartdart-async

How can I merge multiple Streams into a higher level Stream?


I have two streams, Stream<A> and Stream<B>. I have a constructor for a type C that takes an A and a B. How do I merge the two Streams into a Stream<C>?


Solution

  • You can use StreamZip in package:async to combine two streams into one stream of pairs, then create the C objects from that.

    import "package:async" show StreamZip;
    ...
    Stream<C> createCs(Stream<A> as, Stream<B> bs) =>
      new StreamZip([as, bs]).map((ab) => new C(ab[0], ab[1]));