subscribe<T> method Null safety

dynamic subscribe<T>(
  1. String topic,
  2. EventHandler<T>? handler
)

Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler

  • topic The topic to subscribe to
  • handler The event handler

Implementation

subscribe<T>(String topic, EventHandler<T>? handler) {
  List<EventHandler<T>>? handlers = _callbackMaps[topic];
  if (handler == null) {
    _callbackMaps.remove(topic);
    return;
  }
  if (handlers == null) {
    handlers = [];
    _callbackMaps[topic] = handlers;
  }
  handlers.add(handler);
}