Stream<T>.fromFuture constructor

Stream<T>.fromFuture(Future<T> future)

Creates a new single-subscription stream from the future.

When the future completes, the stream will fire one event, either data or error, and then close with a done-event.

Implementation

factory Stream.fromFuture(Future<T> future) {
  // Use the controller's buffering to fill in the value even before
  // the stream has a listener. For a single value, it's not worth it
  // to wait for a listener before doing the `then` on the future.
  _StreamController<T> controller =
      new _SyncStreamController<T>(null, null, null, null);
  future.then((value) {
    controller._add(value);
    controller._closeUnchecked();
  }, onError: (error, stackTrace) {
    controller._addError(error, stackTrace);
    controller._closeUnchecked();
  });
  return controller.stream;
}