notifyListeners method

  1. @protected
  2. @visibleForTesting
void notifyListeners ()
@protected, @visibleForTesting

Call all the registered listeners.

Call this method whenever the object changes, to notify any clients the object may have. 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.

This method must not be called after dispose has been called.

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 removeListener.

Implementation

@protected
@visibleForTesting
void notifyListeners() {
  assert(_debugAssertNotDisposed());
  if (_listeners != null) {
    final List<VoidCallback> localListeners = List<VoidCallback>.from(_listeners);
    for (VoidCallback listener in localListeners) {
      try {
        if (_listeners.contains(listener))
          listener();
      } catch (exception, stack) {
        FlutterError.reportError(FlutterErrorDetails(
          exception: exception,
          stack: stack,
          library: 'foundation library',
          context: ErrorDescription('while dispatching notifications for $runtimeType'),
          informationCollector: () sync* {
            yield DiagnosticsProperty<ChangeNotifier>(
              'The $runtimeType sending notification was',
              this,
              style: DiagnosticsTreeStyle.errorProperty,
            );
          },
        ));
      }
    }
  }
}