How to merge stream using StreamZip in Dart

此生再无相见时 提交于 2021-01-28 19:52:42

问题


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?


回答1:


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>>>.



来源:https://stackoverflow.com/questions/62955391/how-to-merge-stream-using-streamzip-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!