forEach method

Future forEach(void action(T element))

Executes action on each data event of the stream.

Completes the returned Future when all events of the stream have been processed. Completes the future with an error if the stream has an error event, or if action throws.

Source

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/1.24.3/dart-async/Stream/forEach.html