ProviderScope class Null safety

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.overrideWithValue(FakeRepository()),
      ],
      child: MyApp(),
    ),
  );
});

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

final themeProvider = Provider((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:

Inheritance
Annotations

Constructors

ProviderScope({Key? key, List<Override> overrides = const [], List<ProviderObserver>? observers, required Widget child})
A widget that stores the state of providers.
const

Properties

child Widget
The part of the widget tree that can use Riverpod and has overridden providers.
final
hashCode int
The hash code for this object.
@nonVirtual, read-only, inherited
key Key?
Controls how one widget replaces another widget in the tree.
final, inherited
observers List<ProviderObserver>?
The listeners that subscribes to changes on providers stored on this ProviderScope.
final
overrides List<Override>
Informations on how to override a provider/family.
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() → ProviderScopeState
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
@nonVirtual, inherited

Static Methods

containerOf(BuildContext context, {bool listen = true}) ProviderContainer
Read the current ProviderContainer for a BuildContext.