contextOf<T extends ReactterContext?> static method
T
contextOf<T extends ReactterContext?>(
- BuildContext context, {
- String? id,
- ListenStates<
T> ? listenStates, - @Deprecated("Use `ListenStates` instead.") ListenHooks<
T> ? listenHooks, - bool listen = true,
Returns an instance of T
and sets the BuildContext
to listen for when it should be re-rendered.
Implementation
static T contextOf<T extends ReactterContext?>(
BuildContext context, {
String? id,
ListenStates<T>? listenStates,
// ignore: deprecated_member_use_from_same_package
@Deprecated("Use `ListenStates` instead.") ListenHooks<T>? listenHooks,
bool listen = true,
}) {
final providerInheritedElement =
_getProviderInheritedElement<T>(context, id);
if (providerInheritedElement?._instance == null && null is! T) {
throw ReactterContextNotFoundException(T, context.widget.runtimeType);
}
final instance = providerInheritedElement?._instance as T;
if (!listen || instance == null) {
return instance;
}
/// A way to tell the `BuildContext` that it should be re-rendered
/// when the `ReactterInstance` or the `ReactterHook`s that are being listened
/// change.
context.dependOnInheritedElement(
providerInheritedElement!,
aspect: ReactterDependency<T?>(
id: id,
instance: (listenStates ?? listenHooks) != null ? null : instance,
states: (listenStates ?? listenHooks)?.call(instance).toSet(),
),
);
return instance;
}