Provider<T> class
A Provider that manages the lifecycle of the value it provides by delegating to a pair of Create and Dispose.
It is usually used to avoid making a StatefulWidget for something trivial, such as instantiating a BLoC.
Provider is the equivalent of a State.initState combined with State.dispose. Create is called only once in State.initState. We cannot use InheritedWidget as it requires the value to be constructor-initialized and final.
The following example instantiates a Model
once, and disposes it when
Provider is removed from the tree.
class Model {
void dispose() {}
}
class Stateless extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Provider<Model>(
create: (context) => Model(),
dispose: (context, value) => value.dispose(),
child: ...,
);
}
}
It is worth noting that the create
callback is lazily called.
It is called the first time the value is read, instead of the first time
Provider is inserted in the widget tree.
This behavior can be disabled by passing lazy: false
to Provider.
Testing
When testing widgets that consumes providers, it is necessary to add the proper providers in the widget tree above the tested widget.
A typical test may look like this:
final foo = MockFoo();
await tester.pumpWidget(
Provider<Foo>.value(
value: foo,
child: TestedWidget(),
),
);
Note this example purposefully specified the object type, instead of having
it inferred.
Since we used a mocked class (typically using mockito
), then we have to
downcast the mock to the type of the mocked class.
Otherwise, the type inference will resolve to Provider<MockFoo>
instead of
Provider<Foo>
, which will cause Provider.of<Foo>
to fail.
- Inheritance
Constructors
-
Provider({Key? key, required Create<
T> create, Dispose<T> ? dispose, bool? lazy, TransitionBuilder? builder, Widget? child}) - Creates a value, store it, and expose it to its descendants.
-
Provider.value({Key? key, required T value, UpdateShouldNotify<
T> ? updateShouldNotify, TransitionBuilder? builder, Widget? child}) - Expose an existing value without disposing it.
Properties
- builder → TransitionBuilder?
-
Syntax sugar for obtaining a BuildContext that can read the provider
created.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
inherited
-
buildWithChild(
BuildContext context, Widget? child) → Widget -
A build method that receives an extra
child
parameter.inherited -
createElement(
) → _InheritedProviderElement< T> -
Creates a StatelessElement to manage this widget's location in the tree.
inherited
-
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 Properties
-
debugCheckInvalidValueType
↔ void Function<
T>(T value)? -
A sanity check to prevent misuse of Provider when a variant should be
used instead.
getter/setter pair
Static Methods
-
of<
T> (BuildContext context, {bool listen = true}) → T - Obtains the nearest Provider<T> up its widget tree and returns its value.