registerTo<T extends Event> method

  1. @override
Stream<T> registerTo<T extends Event>([
  1. bool includeLastEvent = false
])
override

Listens for events of Type Event and its subtypes.

The method is called like this: eventBus.registerTo

if includeLastEvent is true the stream will emit the last event with that specific type. If there is no last event it won't to anything.

Implementation

@override
Stream<T> registerTo<T extends Event>([bool includeLastEvent = false]) {
  assert(T.toString() != "Event", "T has to be a subclass of Event!");
  assert(!_eventBusIsAlreadyClosed, "EventBus is already closed");

  /// The name / key of the map for the events
  String eventKeyName = T.toString();

  _streamEventMap[eventKeyName] ??= [];

  EventController<Event> controller =
      _createAndAddController(includeLastEvent, eventKeyName);

  /// If the [previousEvent] parameter is set to true, we add the last event to the [EventController]
  if (includeLastEvent) {
    final lastEvent = _lastEvents[eventKeyName];

    if (lastEvent != null) {
      controller.add(lastEvent);
    }
  }

  return controller.stream
      .where((Event event) => event is T)
      .cast<T>()
      .asBroadcastStream();
}