forEach method
Executes action on each element of this stream.
Completes the returned Future when all elements of this stream have been processed.
If this stream emits an error, or if the call to action throws,
the returned future completes with that error,
and processing stops.
Implementation
Future<void> forEach(void action(T element)) {
_Future future = _Future();
StreamSubscription<T> subscription = this.listen(
null,
onError: future._completeError,
onDone: () {
future._complete(null);
},
cancelOnError: true,
);
subscription.onData((T element) {
_runUserCode<void>(
() => action(element),
(_) {},
_cancelAndErrorClosure(subscription, future),
);
});
return future;
}