of static method
Finds the TreeViewController for the closest TreeView instance that encloses the given context.
If no TreeView encloses the given context, calling this method will cause an assert in debug mode, and throw an exception in release mode.
To return null if there is no TreeView use maybeOf instead.
Typical usage of the TreeViewController.of function is to call it
from within the build
method of a descendant of an TreeView.
When the TreeView is actually created in the same build
function as the callback that refers to the controller, then the
context
argument to the build
function can't be used to find
the TreeViewController (since it's "above" the widget
being returned in the widget tree). In cases like that you can
add a Builder widget, which provides a new scope with a
BuildContext that is "under" the TreeView.
Implementation
static TreeViewController of(BuildContext context) {
final _TreeViewState<Object?>? result =
context.findAncestorStateOfType<_TreeViewState<Object?>>();
if (result != null) {
return result.controller;
}
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'TreeViewController.of() called with a context that does not contain a '
'TreeView.',
),
ErrorDescription(
'No TreeView ancestor could be found starting from the context that '
'was passed to TreeViewController.of(). '
'This usually happens when the context provided is from the same '
'StatefulWidget as that whose build function actually creates the '
'TreeView widget being sought.',
),
ErrorHint(
'There are several ways to avoid this problem. The simplest is to use '
'a Builder to get a context that is "under" the TreeView.',
),
ErrorHint(
'A more efficient solution is to split your build function into '
'several widgets. This introduces a new context from which you can '
'obtain the TreeView. In this solution, you would have an outer '
'widget that creates the TreeView populated by instances of your new '
'inner widgets, and then in these inner widgets you would use '
'TreeViewController.of().',
),
context.describeElement('The context used was'),
]);
}