inheritFromElement method

  1. @override
InheritedWidget inheritFromElement (Element ancestor, { 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 inheritFromWidgetOfExactType, which calls this method after finding the appropriate InheritedElement ancestor.

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

Implementation

@override
InheritedWidget inheritFromElement(Element ancestor, { Object aspect }) {
  assert(ancestor != null);
  assert(() {
    final Type targetType = ancestor.widget.runtimeType;
    if (state._debugLifecycleState == _StateLifecycle.created) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('inheritFromWidgetOfExactType($targetType) or inheritFromElement() 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('inheritFromWidgetOfExactType($targetType) or inheritFromElement() was called after dispose(): $this'),
        ErrorDescription(
          'This error happens if you call inheritFromWidgetOfExactType() 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 '
          'inheritFromWidgetOfExactType() 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 '
          'inheritFromWidgetOfExactType() to ensure the object is still in the '
          'tree.'
        ),
        ErrorHint(
          'This error might indicate a memory leak if '
          'inheritFromWidgetOfExactType() 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.inheritFromElement(ancestor, aspect: aspect);
}