bloomOf<T> method
Reactively subscribes to an ancestor BloomProvider and returns its inner Bloom<T> state.
Exact Type Resolution:
Lookup matches the exact reified type T. Subtypes or base classes do not match
(e.g., BloomProvider<Dog> cannot be resolved via context.bloomOf<Animal>()).
To query by a base class or interface, explicitly provision as BloomProvider<Animal>.
Requires:
- An ancestor BloomProvider of type
Tmust exist in the widget tree when evaluated.
Ensures:
- Registers this BuildContext as a dependent on the ancestor BloomProvider and returns its inner Bloom reactive node.
Throws:
- Throws FlutterError in all build modes if no matching BloomProvider is found in the current hierarchy.
Examples
final counterBloom = context.bloomOf<int>();
Implementation
@pragma('vm:prefer-inline')
Bloom<T> bloomOf<T>() {
final provider = dependOnInheritedWidgetOfExactType<BloomProvider<T>>();
if (provider == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'No ancestor BloomProvider<$T> found in the current BuildContext hierarchy.'),
ErrorDescription(
'BuildContext.bloomOf<$T>() was called with a context that does not contain '
'a BloomProvider<$T> ancestor.\n'
'Lookup is strictly by exact runtime type ($T). InheritedWidget resolution does '
'not match subtypes or base classes (e.g., BloomProvider<Foo> cannot be resolved as FooBase).',
),
ErrorHint(
'Ensure that a BloomProvider<$T> is mounted above this widget in the widget tree, '
'or use context.maybeBloomOf<$T>() if the provider is optional. '
'If providing a subclass for a base class interface, provide it explicitly as '
'BloomProvider<$T>(bloom: instance).',
),
describeElement('The context used was'),
]);
}
return provider.bloom;
}