forEach method

Future forEach<T>(Iterable<T> input, dynamic f(T element))

Perform an operation for each element of the iterable, in turn.

The operation, f, may be either synchronous or asynchronous.

Calls f with each element in input in order. If the call to f returns a Future<T>, the iteration waits until the future is completed before moving to the next element.

Returns a Future that completes with null when all elements have been processed.

Non-Future return values, and completion-values of returned Futures, are discarded.

Any error from f, synchronous or asynchronous, will stop the iteration and will be reported in the returned Future.

Source

static Future forEach<T>(Iterable<T> input, FutureOr f(T element)) {
  var iterator = input.iterator;
  return doWhile(() {
    if (!iterator.moveNext()) return false;
    var result = f(iterator.current);
    if (result is Future) return result.then(_kTrue);
    return true;
  });
}

© 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/Future/forEach.html