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 StatefulComponent 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 InheritedComponent 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 StatelessComponent {
  @override
  Iterable<Component> build(BuildContext context) sync* {
    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 component tree.

This behavior can be disabled by passing lazy: false to Provider.

Testing

When testing components that consumes providers, it is necessary to add the proper providers in the component tree above the tested component.

A typical test may look like this:

final foo = MockFoo();

await tester.pumpComponent(
  Provider<Foo>.value(
    value: foo,
    child: TestedComponent(),
  ),
);

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, Component? child})
Creates a value, store it, and expose it to its descendants.
Provider.value({Key? key, required T value, UpdateShouldNotify<T>? updateShouldNotify, TransitionBuilder? builder, Component? 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 component replaces another component in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build(BuildContext context) Iterable<Component>
Describes the part of the user interface represented by this component.
inherited
buildWithChild(BuildContext context, Component? child) Iterable<Component>
A build method that receives an extra child parameter.
inherited
createElement() → _InheritedProviderElement<T>
Creates a StatelessElement to manage this component's location in the tree.
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<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 component tree and returns its value.