Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter provider changenotifierprovider question

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!

like image 731
Matt123 Avatar asked Jan 23 '20 17:01

Matt123


People also ask

How do you use provider value in Flutter?

Provider<String>(create: (context) => 'Hello Flutter'), This time Flutter will look for a String or text; so we've returned a text: “Hello Flutter”. Once we've passed this generic value we can try to access that value anywhere, in the widget tree. However, that value must be provided there inside the build method.

What is a changenotifier class in flutter?

This class is basically a provider-wrapper over a class that implements ChangeNotifier. According to the Flutter docs, a ChangeNotifier is 'a class that can be extended or mixed in that provides a change notification API using VoidCallback for notifications.' In practical terms, other objects can listen to a ChangeNotifier object.

What is the difference between changenotifierprovider and listenableprovider?

A ChangeNotifierProvider however is a specification of a type of Provider for Listenable Objects (models), which will then listen to the model and tell the widgets that depend on it to rebuild if the model’s value changes. ListenableProvider: A specific provider for Listenable object.

What is the difference between changenotifier and provider?

whats the difference between these 2 widgets and its necessary to use ChangeNotifier in every Provider or there is many ways to use Provider? Show activity on this post. Provider exposes a value down the widget tree so that children can have access to it, regardless their location (but still, they must be at least one level below the provider).

Why can't I build this changenotifierprovider<themeswitch> widget?

This ChangeNotifierProvider<ThemeSwitch> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building.


1 Answers

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.

like image 83
Naslausky Avatar answered Oct 05 '22 05:10

Naslausky