dispatchObjectEvent method

void dispatchObjectEvent(
  1. ObjectEvent event
)

Dispatch a new object event to listeners.

Exceptions thrown by listeners will be caught and reported using FlutterError.reportError.

Listeners added during an event dispatching, will start being invoked for next events, but will be skipped for this event.

Listeners, removed during an event dispatching, will not be invoked after the removal.

Only call this when kFlutterMemoryAllocationsEnabled is true.

Implementation

void dispatchObjectEvent(ObjectEvent event) {
  if (!kFlutterMemoryAllocationsEnabled) {
    return;
  }
  final List<ObjectEventListener?>? listeners = _listeners;
  if (listeners == null || listeners.isEmpty) {
    return;
  }

  _activeDispatchLoops++;
  final int end = listeners.length;
  for (int i = 0; i < end; i++) {
    try {
      listeners[i]?.call(event);
    } catch (exception, stack) {
      final String type = event.object.runtimeType.toString();
      FlutterError.reportError(FlutterErrorDetails(
        exception: exception,
        stack: stack,
        library: 'foundation library',
        context: ErrorDescription('MemoryAllocations while '
        'dispatching notifications for $type'),
        informationCollector: () => <DiagnosticsNode>[
          DiagnosticsProperty<Object>(
            'The $type sending notification was',
            event.object,
            style: DiagnosticsTreeStyle.errorProperty,
          ),
        ],
      ));
    }
  }
  _activeDispatchLoops--;
  _tryDefragmentListeners();
}