Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Why is Provider a better option than a class called AppGlobal with only static singletons?

From what I understand about the flutter package, Provider, is that it's a way to share objects between widgets. I know another way of doing this is to create a class, say AppGlobal, and define various static variables that the whole app could use. It's suggested that Provider is a better way of doing that, but I don't understand why that is.

like image 330
JVE999 Avatar asked Feb 08 '20 21:02

JVE999


People also ask

When should I use providers flutter?

One of the main reasons to prefer Provider over Statefulwidget s is that, using Provider , you will rebuild only the widgets that needs that value (the Consumers ) while the other will not be rebuilt. Instead when you call setState the whole build function of the widget will be called.

What is the use of singleton class in flutter?

The singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. The page also says that the singleton pattern solves problems by allowing it to: Ensure that a class only has one instance. Easily access the sole instance of a class.

What is static in flutter?

The static keyword is used for a class-level variable and method that is the same for every instance of a class, this means if a data member is static, it can be accessed without creating an object. The static keyword allows data members to persist Values between different instances of a class.

What is scoped access flutter?

Scoped access is about making objects available to an entire widget sub-tree. Flutter already uses this technique. If you ever called Navigator. of(context) , MediaQuery. of(context) or Theme.


1 Answers

An answer to the question should take different aspects into account:

  1. Testability - not much of a difference. Both cases require code change to replace either the singleton itself or the "provided singleton"
  2. Code Coupling - not much of a difference either (see comment on testability)
  3. Scoping - Singletons most often live through the lifecycle of the whole application. It would be error prone to manage a singleton for some widget subtree. Here provider definitely has its strengths by taking care of creation and disposal.
  4. UI updating - when using singletons this must be completely hand coded with setState which will produce lots of error prone boilerplate code. Provider provides this already under the hood.
  5. Listeners - changing state in one part of the application should notify all consumers of that state. With singletons this has to be built by hand. Provider provides this already under the hood.
  6. Lazy Loading - By default, values are lazy-loaded, which means they are called the first time the value is read instead of the first time the provider is created. This can be disabled by lazy: false

Hope this answers the question in more depth.

like image 146
Jemolah Avatar answered Oct 06 '22 00:10

Jemolah