ProviderContainer constructor Null safety

ProviderContainer(
  1. {ProviderContainer? parent,
  2. List<Override> overrides = const [],
  3. List<ProviderObserver>? observers}
)

An object that stores the state of the providers and allows overriding the behavior of a specific provider.

If you are using Flutter, you do not need to care about this object (outside of testing), as it is implicitly created for you by ProviderScope.

Implementation

ProviderContainer({
  ProviderContainer? parent,
  List<Override> overrides = const [],
  List<ProviderObserver>? observers,
})  : _debugOverridesLength = overrides.length,
      depth = parent == null ? 0 : parent.depth + 1,
      _parent = parent,
      _observers = [
        ...?observers,
        if (parent != null) ...parent._observers,
      ],
      _stateReaders = {
        if (parent != null)
          for (final entry in parent._stateReaders.entries)
            if (!entry.value.isDynamicallyCreated) entry.key: entry.value,
      },
      _root = parent?._root ?? parent {
  assert(() {
    _debugId = '${_debugNextId++}';
    RiverpodBinding.debugInstance.containers = {
      ...RiverpodBinding.debugInstance.containers,
      _debugId: this,
    };
    return true;
  }(), '');

  if (parent != null) {
    parent._children.add(this);
    _overrideForFamily.addAll(parent._overrideForFamily);
  }

  for (final override in overrides) {
    if (override is ProviderOverride) {
      _overrideForProvider[override._origin] = override._override;
      _stateReaders[override._origin] = _StateReader(
        origin: override._origin,
        override: override._override,
        container: this,
        isDynamicallyCreated: false,
      );
    } else if (override is FamilyOverride) {
      _overrideForFamily[override.overriddenFamily] = _FamilyOverrideRef(
        override,
        this,
      );
    }
  }
}