share method Null safety

Stream<T> share()

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.


// Convert a single-subscription fromIterable stream into a broadcast
// stream
final stream =  Stream.fromIterable([1, 2, 3]).share();

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

// Stop emitting items from the source stream and close the underlying
// PublishSubject


Stream<T> share() => publish().refCount();