dispose method Null safety

void dispose()

Release all the resources associated with this ProviderContainer.

This will destroy the state of all providers associated to this ProviderContainer and call Ref.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;
  }(), '');

  _parent?._children.remove(this);

  _disposed = true;

  for (final element in getAllProviderElementsInOrder().toList().reversed) {
    element.dispose();
  }

  if (_root == null) _scheduler.dispose();
}