Provider vs. InheritedWidget

落爺英雄遲暮 提交于 2020-05-28 03:40:21

问题


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?


回答1:


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.




回答2:


Provider is not a must, but should.

First of all, it's officially supported and flexible-powerful enough for almost every state-management.

It might not be right to sum up like that InheritedWidget with dispose because Provider has too many different usecases and inherits some optimizations probably you won't find anywhere else.

For example, it handles very well dispose methods for situations you create automated providers etc. so, better don't narrow down the efficiency like just having dispose parameter, it's also handles some disposes implicitly.

Another example, if you use InheritedWidget in large application, build methods always execute full body. But with Provider you have Consumer Widget which is can be very specific to control specific blocks of build method, so you have more efficieny. So, at the end, especially in large projects you'll see huge benefits of using Provider.

The problem is since Flutter was intented to be UI Framework at first, with default things we won't have flexible and powerful enough state-management solution.

Lastly, since you'll need different state-management solutions for different projects, one package-for-all scenario is invaluable imo.



来源:https://stackoverflow.com/questions/57157823/provider-vs-inheritedwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!