ProviderScope constructor

const ProviderScope({
  1. Key? key,
  2. List<Override> overrides = const [],
  3. List<ProviderObserver>? observers,
  4. required Widget child,
})

A widget that stores the state of providers.

All Flutter applications using Riverpod must contain a ProviderScope at the root of their widget tree. It is done as followed:

void main() {
  runApp(
    // Enabled Riverpod for the entire application
    ProviderScope(
      child: MyApp(),
    ),
  );
}

It optionally possible to specify overrides to change the behavior of some providers. This can be useful for testing purposes:

testWidgets('Test example', (tester) async {
  await tester.pumpWidget(
    ProviderScope(
      overrides: [
        // override the behavior of repositoryProvider to provide a fake
        // implementation for test purposes.
        repositoryProvider.overrideWithProvider(
          Provider((_) => FakeRepository()),
        ),
      ],
      child: MyApp(),
    ),
  );
});

Similarly, it is possible to insert other ProviderScope anywhere inside the widget tree to override the behavior of a ScopedProvider for only a part of the application:

final themeProvider = ScopedProvider((ref) => MyTheme.light());

void main() {
  runApp(
    ProviderScope(
      child: MaterialApp(
        // Home uses the default behavior for all providers.
        home: Home(),
        routes: {
          // Overrides themeProvider for the /gallery route only
          '/gallery': (_) => ProviderScope(
            overrides: [
              themeProvider.overrideWithValue(MyTheme.dark()),
            ],
          ),
        },
      ),
    ),
  );
}

See also:

Implementation

const ProviderScope({
  Key? key,
  this.overrides = const [],
  this.observers,
  required this.child,
}) : super(key: key);