Exploring Inherited Widget in Flutter | by Sumit Ghosh | Jan, 2022

you have several wNested in a widget tree, and you’re trying to access data somewhere at the top of the tree.

class InheritedNetworkHandler extends InheritedWidget { }
  • updateShouldNotify, which is not an applicable method.
class InheritedNetworkHandler extends InheritedWidget {  @override
bool updateShouldNotify(InheritedWidget oldWidget) {
return true;
}
}
class InheritedNetworkHandler extends InheritedWidget {  const InheritedNetworkHandler({required Widget child}) :      super(child: child);  @override
bool updateShouldNotify(covariant InheritedWidget oldWidget) { return true;
}
}
class InheritedNetworkHandler extends InheritedWidget {  // Data hold by inherited widget
final bool isNetworkAvailable;
const InheritedNetworkHandler({required this.isNetworkAvailable, required Widget child}): super(child: child); @override
bool updateShouldNotify(covariant InheritedWidget oldWidget) { return true;
}
}
class ChildWidget extends StatelessWidget {
const ChildWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {

// Something new?
final bool? isNetworkAvailable = context.dependOnInheritedWidgetOfExactType<InheritedNetworkHandler>()?.isNetworkAvailable;

return Container();
}
}
class InheritedNetworkHandler extends InheritedWidget {
...
static InheritedNetworkHandler? of(BuildContext context) => context.dependOnInheritedWidgetOfExactType<InheritedNetworkHandler>();
}
class ChildWidget extends StatelessWidget {
const ChildWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {

// Updated version
final bool? isNetworkAvailable = InheritedNetworkHandler.of(context)?.isNetworkAvailable;;

return Container();
}
}
  • Navigator.of(context)

Leave a Comment