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 unnecessaryly rebuilding dependants when nothing changed. Defaults to (previous, next) => previous != next. See InheritedWidget.updateShouldNotify for more informations.

class Model {
  void dispose() {}

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


Provider({Key key, @required ValueBuilder<T> builder, Disposer<T> dispose, Widget child })
Allows to specify parameters to Provider.
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. [...]