Provider – using less boilerplate code than InheritedWidget
If you understood InheritedWidget
well from the previous chapter, you will be able to grasp Provider
much faster. This is due to the fact that Provider
functions just the same as InheritedWidget
, but with much less boilerplate code. You won't need to set up a separate inherited widget class and an intermediate class that uses setState
functions within itself. You will only be using the Provider
class wrapped around your root widget along with accessing it inside any of the children widgets using the .of
method. Let's revisit this and try the same counter app example using Provider
.
Adding a Provider dependency in a sample application
Create a new application using any name you want just like we did in the previous chapter:
flutter create any_name_you_wish
You will get the same counter example application with the default setState
management technique implemented to update the counter value.