toFuture method

Future<T> toFuture()

Returns a Future that completes with the first value of this Observable.

Implementation

Future<T> toFuture() {
  final disposable = CompositeDisposable();
  final completer = Completer<T>();
  final observer = Observer<T>(
    next: (value) {
      completer.complete(value);
      disposable.dispose();
    },
    error: (error, stackTrace) {
      completer.completeError(error, stackTrace);
      disposable.dispose();
    },
    complete: () {
      completer.completeError(TooFewError());
      disposable.dispose();
    },
  );
  disposable.add(observer);
  disposable.add(subscribe(observer));
  return completer.future;
}