Exploring Inherited Widget: The Powerful State Management Solution in Flutter

Salman Bediya
5 min readMar 28, 2023

Flutter is a popular open-source mobile application development framework that helps in creating beautiful and fast applications for both iOS and Android platforms. Flutter comes with a unique feature called “state management” that allows developers to manage and update the application state with ease. In this article, we will take a deep dive into the back story of state management in Flutter and explore one of the most important concepts in Flutter state management, the Inherited Widget.

The Back Story of State Management in Flutter

State management is a process of managing the state of an application. In Flutter, the state is the data that changes over time and is essential for building interactive and responsive applications. Flutter provides different techniques for state management, and choosing the right approach can be crucial for the performance and scalability of the application.

Initially, Flutter provided only one way of managing the state, which is through the StatefulWidget. The StatefulWidget is a stateful widget that can be used to store and manage the state of the application. However, as the complexity of the application grows, managing the state with StatefulWidget becomes challenging and error-prone. To overcome this issue, Flutter introduced several state management solutions, including Provider, BLoC, MobX, Redux, GetX, and more.

Inherited Widget in Flutter

One of the most important concepts in Flutter state management is the Inherited Widget. An Inherited Widget is a special type of widget that allows data to be passed down the widget tree to its descendants. The data can then be accessed by any widget in the subtree.

Inherited Widget is a powerful tool for managing the state of an application. It allows developers to share data between widgets without having to pass it explicitly down the widget tree. Inherited Widget is also optimized for performance, as it only rebuilds the subtree that is affected by the change in data.

To understand Inherited Widgets better, let’s look at an example.

Example of Inherited Widget

Suppose we have an application that displays the name and age of a person. We can use Inherited Widget to share the person’s data between different widgets in the application.

First, we need to create a Person model that contains the name and age of the person.

class Person {
final String name;
final int age;

Person(this.name, this.age);
}

Next, we create an Inherited Widget called PersonData that holds the person’s data and passes it down the widget tree.

class PersonData extends InheritedWidget {
final Person person;

PersonData({Key key, @required this.person, @required Widget child}) : super(key: key, child: child);

static PersonData of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<PersonData>();
}

@override
bool updateShouldNotify(PersonData oldWidget) {
return person != oldWidget.person;
}
}

In the PersonData widget, we define a constructor that takes a person object and a child widget. The person object is the data we want to share, and the child widget is the subtree where the person data can be accessed.

The static method of() returns the nearest ancestor of type PersonData. This method allows us to access the person data from any widget in the subtree.

Finally, we override the updateShouldNotify() method to indicate whether the widget needs to be rebuilt when the person data changes. This method compares the old and new person data and returns true if they are different.

Now, we can use the PersonData widget to pass the person data down the widget tree.

class PersonInfo extends StatelessWidget {
@override
Widget build(BuildContext context) {
final personData = PersonData.of(context).person;
return Column(
children: [
Text("Name: ${personData.name}"),
Text("Age: ${personData.age}"),
],
);
}
}

In the PersonInfo widget, we use the PersonData widget to access the person data and display it in a Column widget.

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PersonData(
person: Person("John Doe", 30),
child: Scaffold(
appBar: AppBar(
title: Text("Inherited Widget Example"),
),
body: PersonInfo(),
),
);
}
}

In the MyApp widget, we wrap the PersonInfo widget with the PersonData widget and provide it with the person data.

Now, when the person data changes, only the widgets that depend on it will be rebuilt, which makes the application more efficient and performant.

Advantages of Inherited Widget

Inherited Widget provides several advantages over other state management solutions in Flutter.

  • Simple and efficient — Inherited Widget is easy to implement and optimized for performance.
  • Easy to share data — Inherited Widget allows developers to share data between widgets without having to pass it explicitly down the widget tree.
  • Granular updates — Inherited Widget only rebuilds the subtree that is affected by the change in data, which makes the application more efficient and performant.
  • Flexible — Inherited Widget can be used in combination with other state management solutions in Flutter, such as Provider and BLoC.

Conclusion

Inherited Widget is a powerful tool for managing the state of an application in Flutter. It allows developers to share data between widgets efficiently and performantly. Inherited Widget is easy to implement and can be used in combination with other state management solutions in Flutter. As a Flutter developer, it is essential to understand Inherited Widget and how to use it to build efficient and performant applications.

Hope you have enjoyed it!
Thanks for reading!

Keep supporting and don’t forget to applaud 👏 on the article if it helps you.

Other Famous Articles:

--

--

Salman Bediya

Developer | Open Source Contributor | Creator | Mentor | Public Speaker | Blogger | Entrepreneur | YouTuber. Let's Flutter with Dart