dispatchCustomEvent method

void dispatchCustomEvent(
  1. String type, {
  2. Object? detail,
  3. bool bubbles = true,
  4. bool composed = true,
  5. bool cancelable = true,
})

Dispatches a custom DOM event from the host element with type and detail.

The detail payload is converted to JavaScript via dartToJsValue. By default, bubbles: true and composed: true are enabled so the event traverses both normal DOM boundaries and Shadow DOM boundaries.

context.dispatchCustomEvent('change', detail: {'value': 42, 'valid': true});

Implementation

void dispatchCustomEvent(
  String type, {
  Object? detail,
  bool bubbles = true,
  bool composed = true,
  bool cancelable = true,
}) {
  final detailJs = dartToJsValue(detail);
  final eventInit = _newJsObject();
  _reflectSet(eventInit, 'detail', detailJs);
  _reflectSet(eventInit, 'bubbles', bubbles.toJS);
  _reflectSet(eventInit, 'composed', composed.toJS);
  _reflectSet(eventInit, 'cancelable', cancelable.toJS);

  final event = web.CustomEvent(type, eventInit as web.CustomEventInit);
  host.dispatchEvent(event);
}