DisposableFuture<T>.first constructor

DisposableFuture<T>.first(
  1. Stream<T> stream
)

Returns a disposable version of stream.first.

Implementation

factory DisposableFuture.first(Stream<T> stream) {
  final completer = Completer<T>.sync();
  late StreamSubscription<Object?> subscription;
  subscription = stream.listen((value) {
    subscription.cancel();
    completer.complete(value);
  }, onError: completer.completeError);
  return DisposableFuture<T>(completer.future, subscription.cancel);
}