shareValueSeeded method

ValueStream<T> shareValueSeeded(
  1. T seedValue
)

Convert the current Stream into a new ValueStream that can be listened to multiple times, providing an initial value. 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. It's also useful for providing sync access to the latest emitted value.

It will replay the latest emitted value to any new listener.

Example

// Convert a single-subscription fromIterable stream into a broadcast
// stream that will emit the latest value to any new listeners
final stream =  Stream.fromIterable([1, 2, 3]).shareValueSeeded(0);

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

// Synchronously print the latest value
print(stream.value);

// Subscribe again later. This will print 3 because it receives the last
// emitted value.
final subscription2 = stream.listen(print);
await Future(() {});

// Stop emitting items from the source stream and close the underlying
// BehaviorSubject by cancelling all subscriptions.
subscription.cancel();
subscription2.cancel();

Implementation

ValueStream<T> shareValueSeeded(T seedValue) =>
    publishValueSeeded(seedValue).refCount();