dependOnInheritedElement method Null safety

  1. @override
InheritedWidget dependOnInheritedElement(
  1. Element ancestor,
  2. {Object? aspect}
)
override

Registers this build context with ancestor such that when ancestor's widget changes this build context is rebuilt.

Returns ancestor.widget.

This method is rarely called directly. Most applications should use dependOnInheritedWidgetOfExactType, which calls this method after finding the appropriate InheritedElement ancestor.

All of the qualifications about when dependOnInheritedWidgetOfExactType can be called apply to this method as well.

Implementation

@override
InheritedWidget dependOnInheritedElement(Element ancestor, { Object? aspect }) {
  assert(ancestor != null);
  assert(() {
    final Type targetType = ancestor.widget.runtimeType;
    if (state._debugLifecycleState == _StateLifecycle.created) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('dependOnInheritedWidgetOfExactType<$targetType>() or dependOnInheritedElement() was called before ${state.runtimeType}.initState() completed.'),
        ErrorDescription(
          'When an inherited widget changes, for example if the value of Theme.of() changes, '
          "its dependent widgets are rebuilt. If the dependent widget's reference to "
          'the inherited widget is in a constructor or an initState() method, '
          'then the rebuilt dependent widget will not reflect the changes in the '
          'inherited widget.',
        ),
        ErrorHint(
          'Typically references to inherited widgets should occur in widget build() methods. Alternatively, '
          'initialization based on inherited widgets can be placed in the didChangeDependencies method, which '
          'is called after initState and whenever the dependencies change thereafter.',
        ),
      ]);
    }
    if (state._debugLifecycleState == _StateLifecycle.defunct) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('dependOnInheritedWidgetOfExactType<$targetType>() or dependOnInheritedElement() was called after dispose(): $this'),
        ErrorDescription(
          'This error happens if you call dependOnInheritedWidgetOfExactType() on the '
          'BuildContext for a widget that no longer appears in the widget tree '
          '(e.g., whose parent widget no longer includes the widget in its '
          'build). This error can occur when code calls '
          'dependOnInheritedWidgetOfExactType() from a timer or an animation callback.',
        ),
        ErrorHint(
          'The preferred solution is to cancel the timer or stop listening to the '
          'animation in the dispose() callback. Another solution is to check the '
          '"mounted" property of this object before calling '
          'dependOnInheritedWidgetOfExactType() to ensure the object is still in the '
          'tree.',
        ),
        ErrorHint(
          'This error might indicate a memory leak if '
          'dependOnInheritedWidgetOfExactType() is being called because another object '
          'is retaining a reference to this State object after it has been '
          'removed from the tree. To avoid memory leaks, consider breaking the '
          'reference to this object during dispose().',
        ),
      ]);
    }
    return true;
  }());
  return super.dependOnInheritedElement(ancestor as InheritedElement, aspect: aspect);
}