Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provider vs. InheritedWidget

Am I wrong or if we just want to pass a value down the Widget tree, Provider is just an InheritedWidget with a dispose method?

like image 273
Michel Feinstein Avatar asked Jul 23 '19 06:07

Michel Feinstein


People also ask

How is InheritedWidget different from provider?

If you use InheritedWidget in large application, build methods always rebuilds whole build method. But with Provider you have Consumer widget which is can be very specific to control specific blocks of build method, so you have more efficiency. Also listeners have less complexity than InheritedWidgets'(O(N) vs O(N²)).

What is an InheritedWidget?

InheritedWidget is a base class that allows classes that extend it to propagate information down the tree efficiently. Basically, it works by notifying registered build contexts if there is any change. Therefore, the descendant widgets that depend on it will only be rebuilt if necessary.

What is a provider architecture?

At an abstract level, we can think of the provider architecture as implementing both dependency injection and data binding. In this architecture, one object (the provider widget) supplies the dependencies (the values) on which another object (the dependent widget) relies, giving them to the other object.

When should I use provider in 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.


1 Answers

Yes. Provider is indeed mostly features based on Inheritedwidgets.

If you want to make your own, then that's fine. But you'll quickly realize that, without provider, you'll have hundreds of useless repetitive lines.

Provider basically takes the logic of InheritedWidgets, but reduce the boilerplate to the strict minimum.

like image 114
Rémi Rousselet Avatar answered Sep 29 '22 19:09

Rémi Rousselet