What problem does InheritedWidget solve?
InheritedWidget allows widgets deep in the tree to directly access shared data without having it passed through every intermediate widget's constructor.
Share data down the widget tree without passing through constructors
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
InheritedWidget is the built-in mechanism in Flutter for sharing data down the widget tree without passing it through every constructor. This pattern is often called "prop drilling" avoidance.
It is the foundation that packages like Provider, Riverpod, and Theme are all built on.
1// ── Creating an InheritedWidget ──2class AppData extends InheritedWidget {3final String username;4final int score;56const AppData({7super.key,8required this.username,9required this.score,10required super.child,11});1213// Access from anywhere in the subtree14static AppData of(BuildContext context) {15return context.dependOnInheritedWidgetOfExactType<AppData>()!;16}1718// Rebuild consumers when data changes?19@override20bool updateShouldNotify(AppData oldWidget) {21return username != oldWidget.username || score != oldWidget.score;22}23}
1// ── Providing data at the top ──2void main() {3runApp(4AppData(5username: 'Flutter Dev',6score: 1500,7child: const MaterialApp(home: HomeScreen()),8),9);10}1112// ── Consuming data anywhere in the tree ──13class ScoreDisplay extends StatelessWidget {14@override15Widget build(BuildContext context) {16final data = AppData.of(context); // access from anywhere!17return Text('${data.username}: ${data.score} XP');18}19}2021// ── Flutter's built-in InheritedWidgets you already use ──22// Theme.of(context) : ThemeData23// MediaQuery.of(context) : MediaQueryData24// Navigator.of(context) : NavigatorState
InheritedWidget is powerful but verbose. For app state, use the Provider package which wraps InheritedWidget behind a cleaner API. Use raw InheritedWidget only when building low-level framework code.
Even if you use Provider or Riverpod daily, understanding InheritedWidget explains why context-based access patterns work and when dependency changes trigger rebuilds.
Share data down the widget tree without passing through constructors. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.
Connect each concept to real app features you can build.
Sketch the widget tree first, then code from top to bottom.
Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.
InheritedWidget allows widgets deep in the tree to directly access shared data without having it passed through every intermediate widget's constructor.
updateShouldNotify compares old and new data. Returning true tells Flutter to rebuild all widgets that called dependOnInheritedWidgetOfExactType.
Flutter's built-in .of(context) methods use InheritedWidget. Provider, Riverpod, and other state management packages are also built on it.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue