flutter provider changenotifierprovider question


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

void main() {
      providers: [
        ChangeNotifierProvider(create: (context) => CartModel()),
        Provider(create: (context) => SomeOtherClass()),
      child: MyApp(),

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



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

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

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.

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.

Answered By – Naslausky

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.