Stream<T>.value constructor Null safety

  1. @Since("2.5")
Stream<T>.value(
  1. T value
)

Creates a stream which emits a single data event before closing.

This stream emits a single data event of value and then closes with a done event.

Example:

Future<void> printThings(Stream<String> data) async {
  await for (var x in data) {
    print(x);
  }
}
printThings(Stream<String>.value('ok')); // prints "ok".

The returned stream is effectively equivalent to one created by (() async* { yield value; } ()) or Future<T>.value(value).asStream().

Implementation

@Since("2.5")
factory Stream.value(T value) =>
    (_AsyncStreamController<T>(null, null, null, null)
          .._add(value)
          .._closeUnchecked())
        .stream;