W3cubDocs

/Dart 2

forEach method

Future forEach (void action(T element))

Executes action on each element of the stream.

Completes the returned Future when all elements of the stream have been processed.

If the stream contains an error, or if the call to action throws, the returned future completes with that error, and processing stops.

Implementation

Future forEach(void action(T element)) {
  _Future future = new _Future();
  StreamSubscription subscription;
  subscription = this.listen(
      (T element) {
        // TODO(floitsch): the type should be 'void' and inferred.
        _runUserCode<dynamic>(() => action(element), (_) {},
            _cancelAndErrorClosure(subscription, future));
      },
      onError: future._completeError,
      onDone: () {
        future._complete(null);
      },
      cancelOnError: true);
  return future;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-async/Stream/forEach.html