of method
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.
Implementation
@override
T of(BuildContext context, {bool defaultToGlobal = false}) {
final _inheritedInjected =
context.dependOnInheritedWidgetOfExactType<_InheritedInjected<T>>();
if (_inheritedInjected != null) {
if (_inheritedInjected.globalInjected == this) {
_inheritedInjected.injected.initialize();
return _inheritedInjected.injected.snapValue.state;
} else {
return of(
_inheritedInjected.context,
defaultToGlobal: defaultToGlobal,
);
}
}
if (defaultToGlobal) {
return snapState.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;
}