updateOverrides method

void updateOverrides(
  1. List<Override> overrides
)

Updates the list of provider overrides.

If you are using flutter, this is done implicitly for you by ProviderScope.

Updating a overrideWithValue with a different value will cause the listeners to rebuild.

It is not possible, to remove or add new overrides, only update existing ones.

Implementation

void updateOverrides(List<Override> overrides) {
  if (_disposed) {
    throw StateError(
      'Called updateOverrides on a ProviderContainer that was already disposed',
    );
  }

  assert(
    _debugOverridesLength == overrides.length,
    'Tried to change the number of overrides. This is not allowed – '
    'overrides cannot be removed/added, they can only be updated.',
  );

  List<Override>? unusedOverrides;
  assert(
    () {
      unusedOverrides = [...overrides];
      return true;
    }(),
    '',
  );

  for (final override in overrides) {
    if (override is ProviderOverride) {
      assert(
        () {
          unusedOverrides!.remove(override);
          return true;
        }(),
        '',
      );

      assert(
        _overrideForProvider[override._origin].runtimeType ==
            override._override.runtimeType,
        'Replaced the override of type ${_overrideForProvider[override._origin].runtimeType} '
        'with an override of type ${override._override.runtimeType}, which is different.\n'
        'Changing the kind of override or reordering overrides is not supported.',
      );

      // _stateReaders[origin] cannot be null for overridden providers.
      final reader = _stateReaders[override._origin]!;

      reader.override =
          _overrideForProvider[override._origin] = override._override;

      final element = reader._element;
      if (element == null) continue;

      runUnaryGuarded(element.update, override._override);
    } else if (override is FamilyOverride) {
      assert(
        () {
          unusedOverrides!.remove(override);
          return true;
        }(),
        '',
      );
      // TODO assert family override did not change

      _overrideForFamily[override.overriddenFamily]!.override = override;
    }
  }

  assert(
    unusedOverrides!.isEmpty,
    'Updated the list of overrides with providers that were not overridden before',
  );
}