Issue
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!
Solution
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 Provider
is 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)