InheritedWidget class Null safety

Base class for widgets that efficiently propagate information down the tree.

To obtain the nearest instance of a particular type of inherited widget from a build context, use BuildContext.dependOnInheritedWidgetOfExactType.

Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state.

The following is a skeleton of an inherited widget called FrogColor:
link
class FrogColor extends InheritedWidget {
  const FrogColor({
    super.key,
    required this.color,
    required super.child,
  });

  final Color color;

  static FrogColor? maybeOf(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<FrogColor>();
  }

  static FrogColor of(BuildContext context) {
    final FrogColor? result = maybeOf(context);
    assert(result != null, 'No FrogColor found in context');
    return result!;
  }

  @override
  bool updateShouldNotify(FrogColor oldWidget) => color != oldWidget.color;
}

Implementing the of and maybeOf methods

The convention is to provide two static methods, of and maybeOf, on the InheritedWidget which call BuildContext.dependOnInheritedWidgetOfExactType. This allows the class to define its own fallback logic in case there isn't a widget in scope.

The of method typically returns a non-nullable instance and asserts if the InheritedWidget isn't found, and the maybeOf method returns a nullable instance, and returns null if the InheritedWidget isn't found. The of method is typically implemented by calling maybeOf internally.

Sometimes, the of and maybeOf methods return some data rather than the inherited widget itself; for example, in this case it could have returned a Color instead of the FrogColor widget.

Occasionally, the inherited widget is an implementation detail of another class, and is therefore private. The of and maybeOf methods in that case are typically implemented on the public class instead. For example, Theme is implemented as a StatelessWidget that builds a private inherited widget; Theme.of looks for that private inherited widget using BuildContext.dependOnInheritedWidgetOfExactType and then returns the ThemeData inside it.

Calling the of or maybeOf methods

When using the of or maybeOf methods, the context must be a descendant of the InheritedWidget, meaning it must be "below" the InheritedWidget in the tree.

In this example, the context used is the one from the Builder, which is a child of the FrogColor widget, so this works.
link
// continuing from previous example...
class MyPage extends StatelessWidget {
  const MyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FrogColor(
        color: Colors.green,
        child: Builder(
          builder: (BuildContext innerContext) {
            return Text(
              'Hello Frog',
              style: TextStyle(color: FrogColor.of(innerContext).color),
            );
          },
        ),
      ),
    );
  }
}

In this example, the context used is the one from the MyOtherPage widget, which is a parent of the FrogColor widget, so this does not work, and will assert when FrogColor.of is called.
link
// continuing from previous example...

class MyOtherPage extends StatelessWidget {
  const MyOtherPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FrogColor(
        color: Colors.green,
        child: Text(
          'Hello Frog',
          style: TextStyle(color: FrogColor.of(context).color),
        ),
      ),
    );
  }
}

See also:

  • StatefulWidget and State, for widgets that can build differently several times over their lifetime.
  • StatelessWidget, for widgets that always build the same way given a particular configuration and ambient state.
  • Widget, for an overview of widgets in general.
  • InheritedNotifier, an inherited widget whose value can be a Listenable, and which will notify dependents whenever the value sends notifications.
  • InheritedModel, an inherited widget that allows clients to subscribe to changes for subparts of the value.
Inheritance
Implementers

Constructors

InheritedWidget({Key? key, required Widget child})
Abstract const constructor. This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
const

Properties

child Widget
The widget below this widget in the tree.
finalinherited
hashCode int
The hash code for this object.
read-onlyinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

Methods

createElement() InheritedElement
Inflates this configuration to a concrete instance.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited
updateShouldNotify(covariant InheritedWidget oldWidget) bool
Whether the framework should notify widgets that inherit from this widget.

Operators

operator ==(Object other) bool
The equality operator.
inherited