notifyActionListeners method

  1. @protected
  2. @visibleForTesting
void notifyActionListeners()

Call all the registered listeners.

Subclasses should call this method whenever the object changes, to notify any clients the object may have changed. Listeners that are added during this iteration will not be visited. Listeners that are removed during this iteration will not be visited after they are removed.

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

Surprising behavior can result when reentrantly removing a listener (i.e. in response to a notification) that has been registered multiple times. See the discussion at removeActionListener.

Implementation

@protected
@visibleForTesting
@pragma('vm:notify-debugger-on-exception')
void notifyActionListeners() {
  if (_listeners.isEmpty) {
    return;
  }

  // Make a local copy so that a listener can unregister while the list is
  // being iterated over.
  final List<ActionListenerCallback> localListeners = List<ActionListenerCallback>.of(_listeners);
  for (final ActionListenerCallback listener in localListeners) {
    InformationCollector? collector;
    assert(() {
      collector = () => <DiagnosticsNode>[
        DiagnosticsProperty<Action<T>>(
          'The $runtimeType sending notification was',
          this,
          style: DiagnosticsTreeStyle.errorProperty,
        ),
      ];
      return true;
    }());
    try {
      if (_listeners.contains(listener)) {
        listener(this);
      }
    } catch (exception, stack) {
      FlutterError.reportError(FlutterErrorDetails(
        exception: exception,
        stack: stack,
        library: 'widgets library',
        context: ErrorDescription('while dispatching notifications for $runtimeType'),
        informationCollector: collector,
      ));
    }
  }
}