W3cubDocs

/Dart 2

Stream<T>.value constructor

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

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

This stream emits a single data event of value and then completes 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;

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-async/Stream/Stream.value.html