shareValue method

ValueObservable<T> shareValue ()

Convert the current Observable into a new ValueObservable 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. It's also useful for providing sync access to the latest emitted value.

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


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

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

// Synchronously print the latest value

// Subscribe again later. This will print 3 because it receives the last
// emitted value.
final subscription2 = observable.listen(print);

// Stop emitting items from the source stream and close the underlying
// BehaviorSubject by cancelling all subscriptions.


ValueObservable<T> shareValue() => publishValue().refCount();