publishValue method
Convert the current Stream into a ValueConnectableStream
that can be listened to multiple times. It will not begin emitting items
from the original Stream until the connect
method is invoked.
This is useful for converting a single-subscription stream into a broadcast Stream that replays the latest emitted value to any new listener. It also provides access to the latest value synchronously.
Example
final source = Stream.fromIterable([1, 2, 3]);
final connectable = source.publishValue();
// Does not print anything at first
connectable.listen(print);
// Start listening to the source Stream. Will cause the previous
// line to start printing 1, 2, 3
final subscription = connectable.connect();
// Late subscribers will receive the last emitted value
connectable.listen(print); // Prints 3
await Future(() {});
// Can access the latest emitted value synchronously. Prints 3
print(connectable.value);
// Stop emitting items from the source stream and close the underlying
// BehaviorSubject
subscription.cancel();
Implementation
ValueConnectableStream<T> publishValue() =>
ValueConnectableStream<T>(this, sync: true);