call method Null safety

Injected<T> call(
  1. BuildContext context,
  2. {bool defaultToGlobal = false}
)

Obtain the Injected model from the nearest InheritedWidget inserted using inherited.

The BuildContext used, will not be registered.

If you want to obtain the state and register it use the of method.

myModel.of(context); // Will return the state and register the BuildContext.
myModel(context); // Will return the Injected model and do not register the BuildContext.

Implementation

Injected<T> call(BuildContext context, {bool defaultToGlobal = false}) {
  final _inheritedInjected = context
      .getElementForInheritedWidgetOfExactType<_InheritedInjected<T>>()
      ?.widget as _InheritedInjected<T>?;

  if (_inheritedInjected != null) {
    if (_inheritedInjected.globalInjected == this) {
      return _inheritedInjected.injected;
    } else {
      return call(
        _inheritedInjected.context,
        defaultToGlobal: defaultToGlobal,
      );
    }
  }
  if (defaultToGlobal) {
    return this;
  }
  throw Exception('No InheritedWidget of type $T is found');

  // return null;
}