DisposableFuture<T>.fromValue constructor

DisposableFuture<T>.fromValue(
  1. T value
)

Returns a disposable version of Future by placing the value in a stream.

Implementation

factory DisposableFuture.fromValue(T value) {
  final completer = Completer<T>.sync();
  bool cancelled = false;
  scheduleMicrotask(() {
    if (!cancelled) {
      completer.complete(value);
    }
  });
  return DisposableFuture(completer.future, () {
    cancelled = true;
  });
}