问题
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