ProviderScope class
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:
- ProviderContainer, a Dart-only class that allows manipulating providers
- UncontrolledProviderScope, which exposes a ProviderContainer to the widget tree without managing its life-cycles.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- ProviderScope
- Annotations
-
- @sealed
Constructors
-
ProviderScope({Key? key, List<
Override> overrides = const [], List<ProviderObserver> ? observers, @Deprecated('Will be removed in 3.0.0. See https://github.com/rrousselGit/riverpod/issues/3261#issuecomment-1973514033') ProviderContainer? parent, 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
- parent → ProviderContainer?
-
Explicitly override the parent ProviderContainer that this ProviderScope
would be a descendant of.
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}) → 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.