Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge stream using StreamZip in Dart

Tags:

flutter

dart

I have two streams:

Stream<List<Order>>  stream1 = pendingStream();
Stream<List<Order>>  stream2 = preparingStream();

I'm trying to use StreamZip from the package:async/async.dart package to merge the streams like so...

Stream<List<Order>> getData() {
     Stream<List<Order>>  stream1 = pendingStream();
     Stream<List<Order>>  stream2 = preparingStream();
    return StreamZip([stream1, stream2]);
}

However it won't compile. Saying:

The element type 'Stream<List<Order>>' can't be assigned to the list type 'Stream<Order>'.

From what I understand StreamZip should accept the two streams? What am I dong wrong?

like image 243
flutter Avatar asked Oct 27 '25 14:10

flutter


1 Answers

You are creating a StreamZip<T> which will emit a List<T> of each event of its merged streams as you can refer in the documentation.

Each of your merged streams emit a List<Order> type, so that means that you will create a merged stream that will emit a List of List.

Basically, you only need to change your return type from Stream<List<Order>> to Stream<List<List<Order>>>.

like image 74
Miguel Ruivo Avatar answered Oct 30 '25 04:10

Miguel Ruivo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!