provider 3.0.0+1

  • Example
  • Installing
  • Versions
  • 99

Build Status pub package codecov Gitter

A dependency injection system built with widgets for widgets. provider is mostly syntax sugar for InheritedWidget, to make common use-cases straightforward.

Migration from v2.0.0 to v3.0.0 #

  • Providers can no longer be instantiated with const.
  • Provider now throws if used with a Listenable/Stream. Consider using ListenableProvider/StreamProvider instead. Alternatively, this exception can be disabled by setting Provider.debugCheckInvalidValueType to null like so:
void main() {
  Provider.debugCheckInvalidValueType = null;

  • All XXProvider.value constructors now use value as parameter name.


ChangeNotifierProvider.value(notifier: myNotifier),


ChangeNotifierProvider.value(value: myNotifier),
  • StreamProvider's default constructor now builds a Stream instead of StreamController. The previous behavior has been moved to the named constructor StreamProvider.controller.


StreamProvider(builder: (_) => StreamController<int>()),


StreamProvider.controller(builder: (_) => StreamController<int>()),

Usage #

Exposing a value #

To expose a variable using provider, wrap any widget into one of the provider widgets from this package and pass it your variable. Then, all descendants of the newly added provider widget can access this variable.

A simple example would be to wrap the entire application into a Provider widget and pass it our variable:

  value: 'Hello World',
  child: MaterialApp(
    home: Home(),

Alternatively, for complex objects, most providers expose a constructor that takes a function to create the value. The provider will call that function only once, when inserting the widget in the tree, and expose the result. This is perfect for exposing a complex object that never changes over time without writing a StatefulWidget.

The following creates and exposes a MyComplexClass. And in the event where Provider is removed from the widget tree, the instantiated MyComplexClass will be disposed.

  builder: (context) => MyComplexClass(),
  dispose: (context, value) => value.dispose()
  child: SomeWidget(),

Reading a value #

The easiest way to read a value is by using the static method Provider.of<T>(BuildContext context). This method will look up in widget tree starting from the widget associated with the BuildContext passed and it will return the nearest variable of type T found (or throw if nothing if found).

Combined with the first example of exposing a value, this widget will read the exposed String and render "Hello World."

class Home extends StatelessWidget {
  Widget build(BuildContext context) {
    return Text(
      /// Don't forget to pass the type of the object you want to obtain to `Provider.of`!

Alternatively instead of using Provider.of, we can use the Consumer widget.

This can be useful for performance optimizations or when it is difficult to obtain a BuildContext descendant of the provider.

  value: 'Hello World',
  child: Consumer<String>(
    builder: (context, value, child) => Text(value),

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

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

And obtain their value independently:

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

MultiProvider #

When injecting many values in big applications, Provider can rapidly become pretty nested:

  value: foo,
  child: Provider<Bar>.value(
    value: bar,
    child: Provider<Baz>.value(
      value: baz,
      child: someWidget,

In that situation, we can use MultiProvider to improve the readability:

  providers: [
    Provider<Foo>.value(value: foo),
    Provider<Bar>.value(value: bar),
    Provider<Baz>.value(value: baz),
  child: someWidget,

The behavior of both examples is strictly the same. MultiProvider only changes the appearance of the code.

Existing providers #

provider exposes a few different kinds of "provider" for different types of objects.

ProviderThe most basic form of provider. It takes a value and exposes it, whatever the value is.
ListenableProviderA specific provider for Listenable object. ListenableProvider will listen to the object and ask widgets which depend on it to rebuild whenever the listener is called.
ChangeNotifierProviderA specification of ListenableProvider for ChangeNotifier. It will automatically call ChangeNotifier.dispose when needed.
ValueListenableProviderListen to a ValueListenable and only expose ValueListenable.value.
StreamProviderListen to a Stream and expose the latest value emitted.
FutureProviderTakes a Future and updates dependents when the future completes.

3.0.0 #

breaking (see the readme for migration steps): #

  • Provider now throws if used with a Listenable/Stream. This can be disabled by setting Provider.debugCheckInvalidValueType to null.
  • The default constructor of StreamProvider has now builds a Stream instead of StreamController. The previous behavior has been moved to StreamProvider.controller.
  • All XXProvider.value constructors now use value as parameter name.
  • Added FutureProvider, which takes a future and updates dependents when the future completes.
  • Providers can no longer be instantiated using const constructors.

non-breaking: #

  • Added ProxyProvider, ListenableProxyProvider, and ChangeNotifierProxyProvider. These providers allows building values that depends on other providers, without loosing reactivity or manually handling the state.
  • Added DelegateWidget and a few related classes to help building custom providers.
  • Exposed the internal generic InheritedWidget to help building custom providers.

2.0.1 #

  • fix a bug where ListenableProvider.value/ChangeNotifierProvider.value /StreamProvider.value/ValueListenableProvider.value subscribed/unsubscribed to their respective object too often
  • fix a bug where ListenableProvider.value/ChangeNotifierProvider.value may rebuild too often or skip some.

2.0.0 #

  • Consumer now takes an optional child argument for optimization purposes.
  • merged Provider and StatefulProvider
  • added a "builder" constructor to ValueListenableProvider
  • normalized providers constructors such that the default constructor is a "builder", and offer a value named constructor.

1.6.1 #

  • Provider.of<T> now crashes with a ProviderNotFoundException when no Provider<T> are found in the ancestors of the context used.

1.6.0 #

  • new: ChangeNotifierProvider, similar to scoped_model that exposes ChangeNotifer subclass and rebuilds dependents only when notifyListeners is called.
  • new: ValueListenableProvider, a provider that rebuilds whenever the value passed to a ValueNotifier change.

1.5.0 #

  • new: Add Consumer with up to 6 parameters.
  • new: MultiProvider, a provider that makes a tree of provider more readable
  • new: StreamProvider, a stream that exposes to its descendants the current value of a Stream.

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


// ignore_for_file: public_member_api_docs
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class Counter with ChangeNotifier {
  int _count = 0;
  int get count => _count;

  void increment() {

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(builder: (_) => Counter()),
      child: Consumer<Counter>(
        builder: (context, counter, _) {
          return MaterialApp(
            supportedLocales: const [Locale('en')],
            localizationsDelegates: [
            home: const MyHomePage(),

class ExampleLocalizations {
  static ExampleLocalizations of(BuildContext context) =>
      Localizations.of<ExampleLocalizations>(context, ExampleLocalizations);

  const ExampleLocalizations(this._count);

  final int _count;

  String get title => 'Tapped $_count times';

class _ExampleLocalizationsDelegate
    extends LocalizationsDelegate<ExampleLocalizations> {
  const _ExampleLocalizationsDelegate(this.count);

  final int count;

  bool isSupported(Locale locale) => locale.languageCode == 'en';

  Future<ExampleLocalizations> load(Locale locale) =>

  bool shouldReload(_ExampleLocalizationsDelegate old) => old.count != count;

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key key}) : super(key: key);

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Title()),
      body: const Center(child: CounterLabel()),
      floatingActionButton: const IncrementCounterButton(),

class IncrementCounterButton extends StatelessWidget {
  const IncrementCounterButton({Key key}) : super(key: key);

  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: Provider.of<Counter>(context).increment,
      tooltip: 'Increment',
      child: const Icon(Icons.add),

class CounterLabel extends StatelessWidget {
  const CounterLabel({Key key}) : super(key: key);

  Widget build(BuildContext context) {
    final counter = Provider.of<Counter>(context);
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        const Text(
          'You have pushed the button this many times:',
          style: Theme.of(context).textTheme.display1,

class Title extends StatelessWidget {
  const Title({Key key}) : super(key: key);

  Widget build(BuildContext context) {
    return Text(ExampleLocalizations.of(context).title);

Use this package as a library

1. Depend on it

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

  provider: ^3.0.0+1

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
3.0.0+1 Jun 11, 2019 Go to the documentation of provider 3.0.0+1 Download provider 3.0.0+1 archive
3.0.0 Jun 7, 2019 Go to the documentation of provider 3.0.0 Download provider 3.0.0 archive
2.0.1+1 May 25, 2019 Go to the documentation of provider 2.0.1+1 Download provider 2.0.1+1 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

All 17 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 Jun 26, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18
  • Flutter: 1.5.4-hotfix.2


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/src/delegate_widget.dart.

Run flutter format to format lib/src/delegate_widget.dart.

Format lib/src/listenable_provider.dart.

Run flutter format to format lib/src/listenable_provider.dart.

Maintenance issues and suggestions

Support latest dependencies. (-5 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.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