ProviderScope class final Core

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's 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

Constructors

ProviderScope({Key? key, List<Override> overrides = const [], List<ProviderObserver>? observers, Retry? retry, 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.
no setterinherited
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
observers → List<ProviderObserver>?
The listeners that subscribes to changes on providers stored on this ProviderScope.
final
overrides → List<Override>
Information on how to override a provider/family.
final
retry → Retry?
The retry logic used by providers associated to this container.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

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.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent 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, int wrapWidth = 65}) → 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.
inherited

Static Methods

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