notifyListeners method
Call all the registered listeners.
Call this method whenever the object changes. 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.
Implementation
@protected
@visibleForTesting
@pragma('vm:notify-debugger-on-exception')
void notifyListeners() {
assert(!_isDisposed, 'A $runtimeType was used after being disposed.');
if (_count == 0) {
return;
}
// To allow potential listeners to recursively call notifyListener, we track
// the number of times this method is called in `_notificationCallStackDepth`.
_notificationCallStackDepth++;
final end = _count;
for (var i = 0; i < end; i++) {
final listenerRef = _listeners[i];
try {
// Also check if the listener was garbage collected mid-loop.
listenerRef?.target?.call();
} catch (exception, stack) {
FlutterError.reportError(
FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'df_pod',
context: ErrorDescription(
'while dispatching notifications for $runtimeType',
),
),
);
}
}
_notificationCallStackDepth--;
if (_notificationCallStackDepth == 0) {
// We really remove the listeners when all notifications are done.
_compactListeners();
}
}