of method Null safety

T of(
  1. BuildContext context,
  2. {bool defaultToGlobal = false}

Obtain the state from the nearest InheritedWidget inserted using inherited.

The BuildContext used, will be registered so that when this Injected model emits a notification, the Element related the the BuildContext will rebuild.

If you want to obtain the state without registering use the call 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.


T of(BuildContext context, {bool defaultToGlobal = false}) {
  final _inheritedInjected =

  if (_inheritedInjected != null) {
    if (_inheritedInjected.globalInjected == this) {
      return _inheritedInjected.injected._state;
    } else {
      return of(
        defaultToGlobal: defaultToGlobal,
  if (defaultToGlobal) {
    return _state;
  throw Exception(
    'No Parent InheritedWidget of type $T is found\n'
    'If you pushed to new route use reInherited method to provide the state '
    'to the new route',
  // return null;