inheritFrom<T extends InheritedModel<Object>> static method

T? inheritFrom<T extends InheritedModel<Object>>(
  1. BuildContext context,
  2. {Object? aspect}
)

Makes context dependent on the specified aspect of an InheritedModel of type T.

When the given aspect of the model changes, the context will be rebuilt. The updateShouldNotifyDependent method must determine if a change in the model widget corresponds to an aspect value.

The dependencies created by this method target all InheritedModel ancestors of type T up to and including the first one for which isSupportedAspect returns true.

If aspect is null this method is the same as context.dependOnInheritedWidgetOfExactType<T>().

If no ancestor of type T exists, null is returned.

Implementation

static T? inheritFrom<T extends InheritedModel<Object>>(BuildContext context, { Object? aspect }) {
  if (aspect == null) {
    return context.dependOnInheritedWidgetOfExactType<T>();
  }

  // Create a dependency on all of the type T ancestor models up until
  // a model is found for which isSupportedAspect(aspect) is true.
  final List<InheritedElement> models = <InheritedElement>[];
  _findModels<T>(context, aspect, models);
  if (models.isEmpty) {
    return null;
  }

  final InheritedElement lastModel = models.last;
  for (final InheritedElement model in models) {
    final T value = context.dependOnInheritedElement(model, aspect: aspect) as T;
    if (model == lastModel) {
      return value;
    }
  }

  assert(false);
  return null;
}