overrideWithProvider method Null safety

ProviderOverride overrideWithProvider(
  1. AlwaysAliveProviderBase<Object?, T> provider

Overrides the behavior of this provider with another provider.

Some common use-cases are:

  • testing, by replacing a service with a fake implementation, or to reach a very specific state easily.
  • multiple environments, by changing the implementation of a class based on the platform or other parameters.

This function should be used in combination with ProviderScope.overrides or ProviderContainer.overrides:

final myService = Provider((ref) => MyService());

    overrides: [
        // Replace the implementation of MyService with a fake implementation
        Provider((ref) => MyFakeService())
    child: MyApp(),


// Cannot be overridden by AutoDisposeProviders
ProviderOverride overrideWithProvider(
  AlwaysAliveProviderBase<Object?, Listened> provider,
) {
  return ProviderOverride(provider, this);