trigger method Null safety

void trigger(
  1. String event,
  2. {dynamic payload,
  3. String? ref}
)

Implementation

void trigger(String event, {dynamic payload, String? ref}) {
  final events = [
    ChannelEvents.close,
    ChannelEvents.error,
    ChannelEvents.leave,
    ChannelEvents.join
  ].map((e) => e.eventName()).toSet();

  if (ref != null && events.contains(event) && ref != joinRef()) {
    return;
  }

  final handledPayload = onMessage(event, payload, ref: ref);
  if (payload != null && handledPayload == null) {
    throw 'channel onMessage callbacks must return the payload, modified or unmodified';
  }

  final filtered = _bindings.where((bind) {
    /// bind all realtime events
    if (bind.event == '*') {
      return event == (payload is Map ? payload['type'] : payload);
    } else {
      return bind.event == event;
    }
  });
  for (final bind in filtered) {
    bind.callback(handledPayload, ref: ref);
  }
}