stateOf<T> method
Retrieves or lazily creates a state of type T, and binds
its ValueState lifecycle to this BuildContext's
widget lifecycle.
onDispose is called with the current value when
the state is removed from its scope and disposed.
Implementation
ValueState<T> stateOf<T>(
T Function() create, {
Key? key,
StateMode mode = .watch,
Function(T)? onDispose,
}) {
final element = getElementForInheritedWidgetOfExactType<StateScope>();
if (element is! StateScopeElement) {
throw FlutterError(
'StateScope was not found above this BuildContext.\n'
'Place StateScope above this context or use ServiceScope.withState.',
);
}
final dependent = this as Element;
final id = StateId(element: dependent, key: key, type: T);
return element.ensureState<T>(dependent, id, mode, create, onDispose);
}