Challenge - 5 Problems
InheritedWidget Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Understanding InheritedWidget Data Access
Given the following Flutter code, what will be printed when the button is pressed?
Flutter
class MyData extends InheritedWidget { final int value; const MyData({required this.value, required Widget child, Key? key}) : super(key: key, child: child); static MyData? of(BuildContext context) { return context.dependOnInheritedWidgetOfExactType<MyData>(); } @override bool updateShouldNotify(MyData oldWidget) => value != oldWidget.value; } class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { final data = MyData.of(context)?.value ?? 0; return ElevatedButton( onPressed: () => print('Value is: $data'), child: Text('Show Value'), ); } } void main() { runApp(MaterialApp( home: MyData( value: 42, child: Scaffold(body: Center(child: MyWidget())), ), )); }
Attempts:
2 left
💡 Hint
Check how MyWidget accesses the value from MyData using context.
✗ Incorrect
The MyWidget uses MyData.of(context) to get the current InheritedWidget instance. Since MyData is an ancestor with value 42, it prints 'Value is: 42'.
❓ lifecycle
intermediate1:30remaining
InheritedWidget Update Notification
What happens when the value in an InheritedWidget changes and updateShouldNotify returns true?
Attempts:
2 left
💡 Hint
Think about how Flutter knows to rebuild widgets that use inherited data.
✗ Incorrect
When updateShouldNotify returns true, Flutter rebuilds widgets that called dependOnInheritedWidgetOfExactType to get the updated data.
🧠 Conceptual
advanced1:30remaining
Why Use InheritedWidget?
Which is the main reason to use an InheritedWidget in Flutter apps?
Attempts:
2 left
💡 Hint
Think about how data can be shared in a big widget tree.
✗ Incorrect
InheritedWidget allows widgets deep in the tree to access shared data without needing to pass it through every intermediate widget.
🔧 Debug
advanced2:00remaining
Diagnosing Missing InheritedWidget Access
Why does calling MyData.of(context) inside the build method of a widget that is NOT a descendant of MyData return null?
Attempts:
2 left
💡 Hint
Think about widget tree structure and where InheritedWidget is placed.
✗ Incorrect
InheritedWidget lookup depends on the widget being a descendant in the tree. If not, the lookup returns null.
expert
2:30remaining
InheritedWidget and Navigation State
If you use an InheritedWidget to share data and then navigate to a new route (screen), what happens to the InheritedWidget data in the new route?
Attempts:
2 left
💡 Hint
Consider where the InheritedWidget is placed relative to Navigator in the widget tree.
✗ Incorrect
InheritedWidget data is only accessible to widgets below it in the tree. If Navigator is above it, new routes won't see that data.