publish<T> method Null safety

dynamic publish<T>(
  1. String topic,
  2. {T? arguments}
)

Publish an Event to given topic.

  • topic The topic to publish to
  • arguments The arguments to pass to the handlers. It's better if custom Structure is used.

Implementation

publish<T>(String topic, {T? arguments}) {
  List<EventHandler<T>>? handlers;
  try {
    handlers = _callbackMaps[topic];
  } catch (_) {
    throw FormatException(
        'Type mismatch between Publisher Arguments and what Subscriber Expects');
  }

  if (handlers == null || handlers.isEmpty) {
    return;
  }

  handlers.forEach((EventHandler<T> handler) {
    try {
      if (arguments != null) {
        handler(arguments);
      } else {
        throw 'arguments null';
      }
    } catch (_) {
      print('Error Occured while executing handler for topic $topic');
      print(_);
    }
  });
}