of static method
The closest instance of DefaultTabController that encloses the given context.
If no instance is found, this method will assert in debug mode and throw an exception in release mode.
Calling this method will create a dependency on the closest
DefaultTabController in the context.
Typical usage is as follows:
TabController controller = DefaultTabController.of(context);
See also:
- DefaultTabController.maybeOf, which is similar to this method, but returns null if no DefaultTabController ancestor is found.
Implementation
// TODO(framework): Add unit tests to this code snippet.
// https://github.com/flutter/flutter/issues/188530
/// Typical usage is as follows:
///
/// ```dart
/// TabController controller = DefaultTabController.of(context);
/// ```
///
/// </callout-box>
///
/// See also:
///
/// * [DefaultTabController.maybeOf], which is similar to this method, but
/// returns null if no [DefaultTabController] ancestor is found.
static TabController of(BuildContext context) {
final TabController? controller = maybeOf(context);
assert(() {
if (controller == null) {
throw FlutterError(
'DefaultTabController.of() was called with a context that does not '
'contain a DefaultTabController widget.\n'
'No DefaultTabController widget ancestor could be found starting from '
'the context that was passed to DefaultTabController.of(). This can '
'happen because you are using a widget that looks for a DefaultTabController '
'ancestor, but no such ancestor exists.\n'
'The context used was:\n'
' $context',
);
}
return true;
}());
return controller!;
}