of static method
- BuildContext context, {
- Axis? axis,
The state from the closest instance of this class that encloses the given context.
Typical usage is as follows:
ScrollableState scrollable = Scrollable.of(context);
Calling this method will create a dependency on the ScrollableState
that is returned, if there is one. This is typically the closest
Scrollable, but may be a more distant ancestor if axis
is used to
target a specific Scrollable.
Using the optional Axis is useful when Scrollables are nested and the
target Scrollable is not the closest instance. When axis
is provided,
the nearest enclosing ScrollableState in that Axis is returned.
This finds the nearest ancestor Scrollable of the context
. This
means that if the context
is that of a Scrollable, it will not find
that Scrollable.
If no Scrollable ancestor is found, then this method will assert in debug mode, and throw an exception in release mode.
See also:
- Scrollable.maybeOf, which is similar to this method, but returns null if no Scrollable ancestor is found.
Implementation
static ScrollableState of(BuildContext context, { Axis? axis }) {
final ScrollableState? scrollableState = maybeOf(context, axis: axis);
assert(() {
if (scrollableState == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'Scrollable.of() was called with a context that does not contain a '
'Scrollable widget.',
),
ErrorDescription(
'No Scrollable widget ancestor could be found '
'${axis == null ? '' : 'for the provided Axis: $axis '}'
'starting from the context that was passed to Scrollable.of(). This '
'can happen because you are using a widget that looks for a Scrollable '
'ancestor, but no such ancestor exists.\n'
'The context used was:\n'
' $context',
),
if (axis != null) ErrorHint(
'When specifying an axis, this method will only look for a Scrollable '
'that matches the given Axis.',
),
]);
}
return true;
}());
return scrollableState!;
}