notifyListeners method
- @pragma('vm:notify-debugger-on-exception')
void
notifyListeners(
)
Implementation
@pragma('vm:notify-debugger-on-exception')
void notifyListeners() {
_garbageCollect();
if (_count == 0) {
return;
}
// Call all listeners anc calculate the call stack depth, in case of
// recursive calling.
_notificationCallStackDepth++;
final end = _count;
for (var i = 0; i < end; i++) {
try {
_listeners[i]?.target?.call();
} catch (exception, stack) {
FlutterError.reportError(
FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'DF Pod',
context: ErrorDescription(
'while dispatching notifications for $runtimeType',
),
informationCollector: () => <DiagnosticsNode>[
DiagnosticsProperty<WeakChangeNotifier>(
'The $runtimeType sending notification was',
this,
style: DiagnosticsTreeStyle.errorProperty,
),
],
),
);
}
}
_notificationCallStackDepth--;
// Remove listeners scheduled for removal by [removeListener].
if (_notificationCallStackDepth == 0 && _reentrantlyRemovedListeners > 0) {
final newLength = _count - _reentrantlyRemovedListeners;
if (newLength * 2 <= _listeners.length) {
final newListeners =
List<WeakReference<VoidCallback>?>.filled(newLength, null);
var newIndex = 0;
for (var i = 0; i < _count; i++) {
final listener = _listeners[i];
if (listener != null) {
newListeners[newIndex++] = listener;
}
}
_listeners = newListeners;
} else {
for (var i = 0; i < newLength; i += 1) {
if (_listeners[i] == null) {
var swapIndex = i + 1;
while (_listeners[swapIndex] == null) {
swapIndex += 1;
}
_listeners[i] = _listeners[swapIndex];
_listeners[swapIndex] = null;
}
}
}
_reentrantlyRemovedListeners = 0;
_count = newLength;
}
}