of static method
- BuildContext context
Finds the ExpansibleController for the closest Expansible instance that encloses the given context.
If no Expansible 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 Expansible use maybeOf instead.
Typical usage of the ExpansibleController.of function is to call it from
within the build
method of a descendant of an Expansible.
Implementation
static ExpansibleController of(BuildContext context) {
final _ExpansibleState? result = context.findAncestorStateOfType<_ExpansibleState>();
assert(() {
if (result == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'ExpansibleController.of() called with a context that does not contain a Expansible.',
),
ErrorDescription(
'No Expansible ancestor could be found starting from the context that was passed to ExpansibleController.of(). '
'This usually happens when the context provided is from the same StatefulWidget as that '
'whose build function actually creates the Expansible 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 Expansible. ',
),
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 Expansible. In this solution, '
'you would have an outer widget that creates the Expansible populated by instances of '
'your new inner widgets, and then in these inner widgets you would use ExpansibleController.of().\n'
'An other solution is assign a GlobalKey to the Expansible, '
'then use the key.currentState property to obtain the Expansible rather than '
'using the ExpansibleController.of() function.',
),
context.describeElement('The context used was'),
]);
}
return true;
}());
return result!.widget.controller;
}