skip to Main Content

what is the reason for using Provider (in a simple app) on top of global variables defined in the main file?
calling Provider has to be embedded in Build methods, so can be a direct access to global varialbes prvided the main is imported

are the widgets calling Provider somehow automatically marked for rebuild if variables are changed?

until now I am using both Provider and direct access to learn the differences

2

Answers


  1. are the widgets calling Provider somehow automatically marked for rebuild if variables are changed?

    Yes! Although maybe not exactly how you think it works. provider is built in a very similar manner to how InheritedWidget is built in the framework. When you call its of method (or context.watch), what you really do is call dependOnInheritedWidgetOfExactType on the BuildContext of your widget (which context you pass to it). This registers the element of that widget to be rebuilt whenever the dependency (the providing widget) changes calling didChangeDependencies and then the build.

    If you provide an ordinary class, it won’t get changed unless you use a Provider.value and changed the object returned in value. If you provide a ChangeNotifier with ChangeNotifierProvider, it will change whenever a notifyListeners on it was called, etc.

    Shall you use a raw InheritedWidget, you would have control over when the dependents (widgets that call Provider.of) shall get updated with a updateShouldNotify boolean getter.

    While using provider/InheritedWidget you can wrap you widget tree with a new Provider/InheritedWidget to change the instance that will be provided down the tree. It can also handle disposing the values when the provider is removed from the tree. With global variables, you’d have to do that manually. Also in widget tests you’d have to set and clean the global variable yourself. To have multiple instances of the same class instantiated for different widget trees you can simply use a few Providers, and with global variables you’d need to create a new variable.

    Please read the below thoroughly to better understand how these work inside:

    Login or Signup to reply.
  2. A global in Dart is not "observable". You can locate the variable, and obtain its current value, but there’s no place to register to be informed of changes. This is particularly important if you are including the data as part of a view, or as part of data that is further observed.

    The various state management solutions (including Provider, and my preferred system, Riverpod) are all mechanisms to implement "observable data", which provides three key features: locatability, current value, and registration of ongoing notifications.

    Riverpod in addition has the ability to "override" the locatability, which comes in handy for testing or dependency injection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search