Implementation

Before we start with the implementation we have to choose the pattern for the state management in our app. There are many ways to reach this goal. We’re going to manage the state in the Flutter way of doing it.

Do you remember how we’ve implemented the list view? We’ve created an active parent widget that gets notified by its inactive widgets in order to update the content as soon as the list of favorites changes. The pattern that we are about to implement today is pretty similar. In the implementation we’re going to use the class InheritedWidget.

InheritedWidget ?

Basically, we can describe InheritedWidget as a parent widget which passes its data down the widget tree. You can access the data from every widget in the app by using the method inheritFromWidgetOfExactType of the BuildContext class.

In previous articles we’ve learned how to access the theme from every widget in the widget tree. We’ve used Theme.of(context) to get styles of the theme and apply them to our widgets.

Here, we are going to do the same. We’re going to access data of the state by StateWidget.of(context) . The class StateWidget is going to wrap a custom InheritedWidget . Furthermore, it is going to provide the of method and do some additional work on the initialization of the state.

Let’s start with the implementation step by step.