18

I am looking at the following code on flutter's website:

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => CartModel()),
        Provider(create: (context) => SomeOtherClass()),
      ],
      child: MyApp(),
    ),
  );
}

I am wondering, what is the difference between Provider and ChangeNotifierProvider?

Thanks!

Matt123
  • 293
  • 1
  • 4
  • 10

2 Answers2

22

From the provider package documentation (all the way down):

Provider: The most basic form of provider. It takes a value and exposes it, whatever the value is.

ListenableProvider: A 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.

ChangeNotifierProvider: A specification of ListenableProvider for ChangeNotifier. It will automatically call ChangeNotifier.dispose when needed.

So, ChangeNotifierProvider is a specific type of Provider which will listen to the object and rebuild its dependent widgets when this object has been updated. Also, it will automatically call the dispose method when needed.

The Provideris the generic provider, without any more complex features, being very much like a optimized Inherited Widget.

Naslausky
  • 3,443
  • 1
  • 14
  • 24
11

Provider

The provider is the most basic of the Provider widget types. You can use it to provide a value (usually a data model object) to anywhere in the widget tree. It will not rebuild the widget tree whenever the value changes. It simply passes the model to its descendants widget in the widget tree.

ChangeNotifierProvider

ChangeNotifierProvider, a subclass of ListenableProvider made for ChangeNotifier. It listens for changes in the model object. It rebuilds the dependents widgets whenever ChangeNotifier.notifyListeners is called.

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70