add method

  1. @override
void add(
  1. Future<T> task

Wait for task to complete.


void add(Future<T> task) {
  if (_closed) throw StateError('The FutureGroup is closed.');

  // Ensure that future values are put into [values] in the same order they're
  // added to the group by pre-allocating a slot for them and recording its
  // index.
  var index = _values.length;

  task.then((value) {
    if (_completer.isCompleted) return null;

    _values[index] = value;

    if (_pending != 0) return null;
    var onIdleController = _onIdleController;
    if (onIdleController != null) onIdleController.add(null);

    if (!_closed) return null;
    if (onIdleController != null) onIdleController.close();
  }).catchError((Object error, StackTrace stackTrace) {
    if (_completer.isCompleted) return null;
    _completer.completeError(error, stackTrace);