shareValueNotReplay method

NotReplayValueStream<T> shareValueNotReplay(
  1. T seedValue
)

Convert the current Stream into a new Stream that can be listened to multiple times. It will automatically begin emitting items when first listened to, and shut down when no listeners remain.

This is useful for converting a single-subscription stream into a broadcast Stream and provides synchronous access to the latest emitted value.

Example

// Convert a single-subscription fromIterable stream into a broadcast
// stream
final stream =  Stream.fromIterable([1, 2, 3]).shareValueNotReplay(0);
print(stream.value); // prints 0

// Start listening to the source Stream. Will start printing 1, 2, 3
final subscription = stream.listen(print);

await subscription.asFuture<void>();
print(stream.value); // prints 3

// Stop emitting items from the source stream and close the underlying
// ValueSubject
subscription.cancel();

Implementation

NotReplayValueStream<T> shareValueNotReplay(T seedValue) =>
    publishValueNotReplay(seedValue).refCount();