removeListener method

  1. @override
void removeListener(
  1. VoidCallback listener
)
override

Remove a previously registered closure from the list of closures that are notified when the object changes.

If the given listener is not registered, the call is ignored. This method is allowed to be called on disposed instances for usability reasons.

Implementation

@override
void removeListener(VoidCallback listener) {
  if (_isDisposed) return;

  for (var i = 0; i < _count; i++) {
    final listenerRef = _listeners[i];
    if (listenerRef?.target == listener) {
      if (_notificationCallStackDepth > 0) {
        // If we are in the middle of a notification, we don't resize the list.
        // We just set the listener to null. It will be cleaned up later.
        _listeners[i] = null;
        _reentrantlyRemovedListeners++;
      } else {
        // When we are outside the notifyListeners iterations, we can
        // effectively shrink the list.
        _removeAt(i);
      }
      break;
    }
  }
}