问题
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