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 {
  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.


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(
    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.



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. [...]


builder TransitionBuilder
Syntax sugar for obtaining a BuildContext that can read the provider created. [...]
final, inherited
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
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


build(BuildContext context) Widget
Describes the part of the user interface represented by this widget. [...]
buildWithChild(BuildContext context, Widget child) Widget
A build method that receives an extra child parameter. [...]
createElement() → _InheritedProviderElement<T>
Creates a StatelessElement to manage this widget's location in the tree. [...]
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. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toDiagnosticsNode({String name, DiagnosticsTreeStyle style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep. [...]
toString({DiagnosticLevel minLevel:}) String
Returns a string representation of this object.
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants. [...]
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object. [...]
toStringShort() String
A short, textual description of this widget.


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

Static Properties

debugCheckInvalidValueType ↔ void Function<T>(T value)
A sanity check to prevent misuse of Provider when a variant should be used instead. [...]
read / write

Static Methods

of<T>(BuildContext context, {bool listen: true}) → T
Obtains the nearest Provider<T> up its widget tree and returns its value. [...]