on<T> method Null safety

Stream<T> on<T>()

Listens for events of Type T and its subtypes.

The method is called like this: myEventBus.on

If the method is called without a type parameter, the Stream contains every event of this EventBus.

The returned Stream is a broadcast stream so multiple subscriptions are allowed.

Each listener is handled independently, and if they pause, only the pausing listener is affected. A paused listener will buffer events internally until unpaused or canceled. So it's usually better to just cancel and later subscribe again (avoids memory leak).

Implementation

Stream<T> on<T>() {
  if (T == dynamic) {
    return streamController.stream as Stream<T>;
  } else {
    return streamController.stream.where((event) => event is T).cast<T>();
  }
}