overrideWithProvider method

Override overrideWithProvider(
  1. Notifier builderOverride(
    1. ProviderReference ref,
    2. Param param
    )
)

Overrides the behavior of a family for a part of the application.

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());

runApp(
  ProviderScope(
    overrides: [
      myService.overrideWithProvider(
        // Replace the implementation of MyService with a fake implementation
        Provider((ref) => MyFakeService())
      ),
    ],
    child: MyApp(),
  ),
);

Implementation

Override overrideWithProvider(
  Notifier Function(ProviderReference ref, Param param) builderOverride,
) {
  return FamilyOverride(
    _notifierFamily,
    (param) {
      return _notifierFamily.create(param as Param, builderOverride, null);
    },
  );
}