asStream method Null safety

Stream<T> asStream()

Creates a Stream containing the result of this operation.

This is like value.asStream(), but if a subscription to the stream is canceled, this operation is as well.

Implementation

Stream<T> asStream() {
  var controller =
      StreamController<T>(sync: true, onCancel: _completer._cancel);

  _completer._inner?.future.then((value) {
    controller.add(value);
    controller.close();
  }, onError: (Object error, StackTrace stackTrace) {
    controller.addError(error, stackTrace);
    controller.close();
  });
  return controller.stream;
}