runOnDispose method

Executes the Ref.onDispose listeners previously registered, then clear the list of listeners.

Implementation

@protected
@visibleForOverriding
@mustCallSuper
void runOnDispose() {
  if (!_mounted) return;
  _mounted = false;

  final subscriptions = _subscriptions;
  if (subscriptions != null) {
    while (subscriptions.isNotEmpty) {
      late int debugPreviousLength;
      if (kDebugMode) {
        debugPreviousLength = subscriptions.length;
      }

      final sub = subscriptions.first;
      sub.close();

      if (kDebugMode) {
        assert(
          subscriptions.length < debugPreviousLength,
          'ProviderSubscription.close did not remove the subscription',
        );
      }
    }
  }

  _onDisposeListeners?.forEach(runGuarded);

  for (final observer in _container.observers) {
    runBinaryGuarded(
      observer.didDisposeProvider,
      _origin,
      _container,
    );
  }

  _onDisposeListeners = null;
  _onCancelListeners = null;
  _onResumeListeners = null;
  _onAddListeners = null;
  _onRemoveListeners = null;
  _onChangeSelfListeners = null;
  _onErrorSelfListeners = null;
  _didCancelOnce = false;
}