Provider<T> class

A Provider that manages the lifecycle of the value it provides by delegating to a pair of ValueBuilder and Disposer.

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

updateShouldNotify can optionally be passed to avoid unnecessarily rebuilding dependents when nothing changed. Defaults to (previous, next) => previous != next. See InheritedWidget.updateShouldNotify for more information.

class Model {
  void dispose() {}

class Stateless extends StatelessWidget {
  Widget build(BuildContext context) {
    return Provider<Model>(
      create: (context) =>  Model(),
      dispose: (context, value) => value.dispose(),
      child: ...,


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 like this:

final foo = MockFoo();

await tester.pumpWidget(
    value: foo,
    child: TestedWidget(),

Note this example purposefully specified the object type, instead of having it infered. 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.

Implemented types


Provider({Key key, @Deprecated('Will be removed as part of 4.0.0, use create instead') ValueBuilder<T> builder, @required ValueBuilder<T> create, Disposer<T> dispose, Widget child })
Creates a value, store it, and expose it to its descendants. [...]
Provider.value({Key key, @required T value, UpdateShouldNotify<T> updateShouldNotify, Widget child })
Allows to specify parameters to Provider.


child Widget
The widget that is below the current Provider widget in the tree. [...]
updateShouldNotify UpdateShouldNotify<T>
User-provided custom logic for InheritedWidget.updateShouldNotify.
delegate ValueStateDelegate<T>
The current state of DelegateWidget. [...]
@protected, read-only, inherited
hashCode → int
The hash code for this object.
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. [...]
cloneWithChild(Widget child) Provider<T>
Clones the current provider with a new child. [...]
createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree. [...]
createState() → _DelegateWidgetState
Creates the mutable state for this widget at a given 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: DiagnosticLevel.debug }) → 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 ==(dynamic other) → bool
The equality operator.

Static Properties

debugCheckInvalidValueType ↔ void Function<T>(T value)
A sanity check to prevent misuse of Provider when a variant should be used. [...]
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. [...]