Provider<T> class
Provides a T to the widgets below it, which find it by its type:
context.read<T>(), context.watch<T>(), context.select, of, a
Consumer.
The value is the one create makes, the first time it is read unless
lazy is false, then handed to dispose when this widget leaves the
tree; with Provider.value one made elsewhere, which it does not
dispose of, and which a new widget may replace by another: the widgets
that watch it are rebuilt when it is not == the one before (or as
updateShouldNotify says).
A value whose changes are to rebuild the widgets below is provided by a
ChangeNotifierProvider, a ListenableProvider or a StreamProvider:
a Provider of a Listenable or a Stream is an error while asserts
are on (debugCheckInvalidValueType).
Settings made once for the app, which a widget below shows:
Provider(
create: (context) => const Settings(user: 'ada'),
child: Builder(
builder: (context) =>
Text('Signed in as ${context.watch<Settings>().user}'),
),
)
See also:
- MultiProvider, for several without nesting them.
ChangeNotifierProvider, for a value whose changes rebuild the widgets below.
- Inheritance
Constructors
-
Provider({required Create<
T> create, Key? key, Dispose<T> ? dispose, bool? lazy, TransitionBuilder? builder, Widget? child}) -
Provides the value
createmakes, handed todispose, if given, when this widget leaves the tree. -
Provider.value({required T value, Key? key, UpdateShouldNotify<
T> ? updateShouldNotify, TransitionBuilder? builder, Widget? child}) -
Provides
value, made and disposed of elsewhere.
Properties
- builder → TransitionBuilder?
-
Builds the child below this provider, from the one given.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Tells this widget apart from its siblings; see Key.
finalinherited
- lazy → bool
-
Whether the value is made only when it is first read; else when this
widget is placed in the tree.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Widget -
Describes this part of the interface; called when the widget is placed
in the tree, whenever its parent gives it a new widget, and when its
element is marked dirty.
inherited
-
buildWithChild(
BuildContext context, Widget? child) → Widget -
What this widget shows around
child.inherited -
createElement(
) → StatelessElement -
Inflates this widget into a new element for the tree.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Adds what describes this widget in its element's description
(Element.describe) to
properties, those not worth showing (a default) left out; an override calls this first. A render object widget adds only what its render object does not show. Only Element.describe calls it: a frame never does.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
-
debugCheckInvalidValueType
↔ void Function<
V>(V value)? -
Checks the value of each Provider, while asserts are on: by default
a Listenable or a Stream is a StateError, since its changes would
rebuild nothing. Null checks none.
getter/setter pair
Static Methods
-
of<
T> (BuildContext context, {bool listen = true}) → T -
The value of the nearest provider of a
Tabovecontext;contextdepends on it whenlisten: rebuilt each time it changes. A ProviderNotFoundException when there is none, unlessTis nullable: then null.