provider 1.4.0

  • Example
  • Installing
  • Versions
  • 97

Build Status pub package codecov

A generic implementation of InheritedWidget. It allows to expose any kind of object, without having to manually write an InheritedWidget ourselves.

Usage #

To expose a value, simply wrap any given part of your widget tree into any of the available Provider as such:

  value: 42,
  child: // ...

Descendants of Provider and now obtain this value using the static Provider.of<T> method:

var value = Provider.of<int>(context);

You can also use Consumer widget to insert a descendant, useful when both creating a Provider and using it:

  value: 42,
  child: Consumer<int>(
    builder: (context, value) => Text(value.toString()),

Note that you can freely use multiple providers with different type together:

  value: 42,
  child: Provider<String>(
    value: 'Hello World',
    child: // ...

And obtain their value independently:

var value = Provider.of<int>(context);
var value2 = Provider.of<String>(context);

Existing Providers: #

Provider #

A simple provider which takes the exposed value directly:

  value: 42,
  child: // ...

StatefulProvider #

A [Provider] that can also create and dispose an object.

It is usually used to avoid making a [StatefulWidget] for something trivial, such as instanciating a BLoC.

[StatefulBuilder] is the equivalent of a [State.initState] combined with [State.dispose]. As such, [valueBuilder] is called only once and is unable to use [InheritedWidget]; which makes it impossible to update the created value.

If this is too limiting, consider instead [HookProvider], which offer a much more advanced control over the created value.

The following example instanciate a Model once, and dispose it when [StatefulProvider] is removed from the tree.

class Model {
  void dispose() {}

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

HookProvider #

A provider which can use hooks from flutter_hooks

This is especially useful to create complex providers, without having to make a StatefulWidget.

The following example uses BLoC pattern to create a BLoC, provide its value, and dispose it when the provider is removed from the tree.

  hook: () {
    final bloc = useMemoized(() => MyBloc());
    useEffect(() => bloc.dispose, [bloc]);
    return bloc;
  child: // ...

1.4.0 #

  • Reintroduced StatefulProvider with a modified prototype. The second argument of valueBuilder and didChangeDependencies have been removed. And valueBuilder is now called only once for the whole life-cycle of StatefulProvider.

1.3.0 #

  • Added Consumer, useful when we need to both expose and consume a value simultaneously.

1.2.0 #

  • Added: HookProvider, a Provider that creates its value from a Hook.
  • Deprecated StatefulProvider. Either make a StatefulWidget or use HookProvider.
  • Integrated the widget inspector, so that Provider widget shows the current value.

1.1.1 #

  • add didChangeDependencies callback to allow updating the value based on an InheritedWidget
  • add updateShouldNotify method to both Provider and StatefulProvider

1.1.0 #

  • onDispose has been added to StatefulProvider
  • BuildContext is now passed to valueBuilder callback


import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';

void main() {

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return Provider(
      value: 42,
      child: Example(),

class Example extends StatelessWidget {
  Widget build(BuildContext context) {
    return Text(Provider.of<int>(context).toString());

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  provider: ^1.4.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:provider/provider.dart';
Version Uploaded Documentation Archive
2.0.1 May 12, 2019 Go to the documentation of provider 2.0.1 Download provider 2.0.1 archive
2.0.0+1 May 3, 2019 Go to the documentation of provider 2.0.0+1 Download provider 2.0.0+1 archive
2.0.0 May 3, 2019 Go to the documentation of provider 2.0.0 Download provider 2.0.0 archive
1.6.1 Feb 21, 2019 Go to the documentation of provider 1.6.1 Download provider 1.6.1 archive
1.6.0 Feb 20, 2019 Go to the documentation of provider 1.6.0 Download provider 1.6.0 archive
1.5.0 Feb 11, 2019 Go to the documentation of provider 1.5.0 Download provider 1.5.0 archive
1.4.0 Feb 4, 2019 Go to the documentation of provider 1.4.0 Download provider 1.4.0 archive
1.3.0 Jan 29, 2019 Go to the documentation of provider 1.3.0 Download provider 1.3.0 archive
1.2.0 Jan 25, 2019 Go to the documentation of provider 1.2.0 Download provider 1.2.0 archive
1.1.1 Oct 22, 2018 Go to the documentation of provider 1.1.1 Download provider 1.1.1 archive

All 14 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on May 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.0
  • pana: 0.12.15
  • Flutter: 1.5.8


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.0
flutter_hooks >=0.2.1 <1.0.0 0.4.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
mockito ^4.0.0
pedantic ^1.4.0