unsubscribe<T> method Null safety

dynamic unsubscribe<T>(
  1. String topic,
  2. {EventHandler<T>? handler}
)

Unsubscribe from the given topic. Your handler will no longer recieve events published to this topic

  • topic The topic to unsubscribe from
  • handler The event handler

Implementation

unsubscribe<T>(String topic, {EventHandler<T>? handler}) {
  if (handler == null) {
    _callbackMaps.remove(topic);
    return;
  }

  final handlers = _callbackMaps[topic];
  if (handlers == null) {
    return;
  }

  // We now need to remove specific handler
  final handlerIndex = handlers.indexOf(handler);

  if (handlerIndex >= 0) {
    handlers.removeAt(handlerIndex);
    if (handlers.length == 0) {
      _callbackMaps.remove(topic);
    }
  }
}