dispose method

void dispose()

Release all the resources associated with this ProviderContainer.

This will destroy the state of all providers associated to this ProviderContainer and call ProviderReference.onDispose listeners.

Implementation

void dispose() {
  if (_disposed) {
    return;
  }
  if (_children.isNotEmpty) {
    throw StateError(
      'Tried to dispose a ProviderContainer that still has children containers.',
    );
  }

  assert(() {
    RiverpodBinding.debugInstance.containers =
        Map.from(RiverpodBinding.debugInstance.containers)..remove(_debugId);
    return true;
  }(), '');

  debugVsyncs.clear();
  _parent?._children.remove(this);

  _disposed = true;

  for (final state in _visitStatesInOrder().toList().reversed) {
    state.dispose();
  }
}