Complete the code to create a custom InheritedWidget class named MyData.
class MyData extends InheritedWidget { final int data; const MyData({required this.data, required Widget child, Key? key}) : super(key: key, child: child); @override bool [1](covariant MyData oldWidget) { return oldWidget.data != data; } }
The updateShouldNotify method tells Flutter when to notify widgets that depend on this InheritedWidget about changes.
Complete the code to access the MyData instance from the widget tree.
final myData = context.dependOnInheritedWidgetOfExactType<[1]>();To get the data, you must specify your custom InheritedWidget class MyData in the generic type.
Fix the error in the method to get the shared data value from MyData.
int getData(BuildContext context) {
final inherited = context.dependOnInheritedWidgetOfExactType<MyData>();
return inherited?.[1] ?? 0;
}The shared value is stored in the data field of MyData, so we access inherited.data.
Fill both blanks to create a widget that uses MyData to show the shared integer.
class DisplayData extends StatelessWidget { @override Widget build(BuildContext context) { final value = context.dependOnInheritedWidgetOfExactType<[1]>()?.[2] ?? 0; return Text('Value: $value'); } }
You must specify MyData to get the inherited widget and access its data field to read the shared value.
Fill all three blanks to create an InheritedWidget that updates dependents when data changes.
class CounterData extends InheritedWidget { final int count; const CounterData({required this.count, required Widget child, Key? key}) : super(key: key, child: child); @override bool [1](covariant CounterData oldWidget) { return oldWidget.[2] != [3]; } }
The method updateShouldNotify compares the old count with the current count to decide if dependents should rebuild.