Stream.fromFutures factory constructor

Stream.fromFutures(Iterable<Future<T>> futures)

Create a stream from a group of futures.

The stream reports the results of the futures on the stream in the order in which the futures complete.

If some futures have completed before calling Stream.fromFutures, their result will be output on the created stream in some unspecified order.

When all futures have completed, the stream is closed.

If no future is passed, the stream closes as soon as possible.

Source

factory Stream.fromFutures(Iterable<Future<T>> futures) {
  _StreamController<T> controller = new StreamController<T>(sync: true);
  int count = 0;
  var onValue = (T value) {
    if (!controller.isClosed) {
      controller._add(value);
      if (--count == 0) controller._closeUnchecked();
    }
  };
  var onError = (error, stack) {
    if (!controller.isClosed) {
      controller._addError(error, stack);
      if (--count == 0) controller._closeUnchecked();
    }
  };
  // The futures are already running, so start listening to them immediately
  // (instead of waiting for the stream to be listened on).
  // If we wait, we might not catch errors in the futures in time.
  for (var future in futures) {
    count++;
    future.then(onValue, onError: onError);
  }
  // Use schedule microtask since controller is sync.
  if (count == 0) scheduleMicrotask(controller.close);
  return controller.stream;
}

© 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/Stream.fromFutures.html