publishState method
Convert the this Stream into a StateConnectableStream
that can be listened to multiple times, providing an initial seeded value.
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 also provides access to the latest value synchronously.
Example
final source = Stream.fromIterable([1, 2, 2, 3, 3, 3]);
final connectable = source.publishState(0);
// 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 not receive anything
connectable.listen(print);
// Can access the latest emitted value synchronously. Prints 3
print(connectable.value);
// Stop emitting items from the source stream and close the underlying
// ValueSubject
subscription.cancel();
Implementation
StateConnectableStream<T> publishState(
T seedValue, {
Equality<T>? equals,
bool sync = true,
}) =>
StateConnectableStream<T>(this, seedValue, equals: equals, sync: sync);