state<St> static method
Get the state, without a StoreConnector
.
Widgets that use this method WILL rebuild whenever the state changes
(unless you pass the notify
parameter as false
).
It's recommended that you define this extension in your own code:
extension BuildContextExtension on BuildContext {
AppState get state => getState<AppState>();
}
This will allow you to write:
var state = context.state;
Implementation
static St state<St>(BuildContext context, {bool notify = true, Object? debug}) {
if (notify) {
final _InheritedUntypedRebuilds? provider =
context.dependOnInheritedWidgetOfExactType<_InheritedUntypedRebuilds>();
if (provider == null)
throw throw _exceptionForWrongStoreType(_typeOf<_InheritedUntypedRebuilds>(), debug: debug);
St state;
try {
state = provider._store.state as St;
} catch (error) {
throw _exceptionForWrongStateType(provider._store.state, St);
}
// We only turn on rebuilds when this `state` method is used for the first time.
// This is to make it faster when this method is not used, which is the
// case if the state is only accessed via StoreConnector.
_InheritedUntypedRebuilds._isOn = true;
return state;
}
// Get the state without rebuilding when the state later changes.
else {
return backdoorInheritedWidget<St>(context, debug: debug).state;
}
}