of<TWidget extends ScopedWidgetBase> static method

TWidget of<TWidget extends ScopedWidgetBase>(
  1. BuildContext context
)

Pass context to retrieve TWidget in the ancestor.

If TWidget does not exist in the ancestor, an error is output.

contextを渡して祖先にあるTWidgetを取り出します。

祖先にTWidgetが存在しない場合はエラーが出力されます。

Implementation

static TWidget of<TWidget extends ScopedWidgetBase>(BuildContext context) {
  _ScopedWidgetScope? scope;
  if (context.widget is TWidget) {
    return context.widget as TWidget;
  }
  do {
    scope = context
        .getElementForInheritedWidgetOfExactType<_ScopedWidgetScope>()
        ?.widget as _ScopedWidgetScope?;
    if (scope == null) {
      break;
    }
    if (scope.state.widget.widget is TWidget) {
      break;
    }
    context = scope.state.context;
  } while (scope.state.widget.widget is! TWidget);
  assert(
    scope != null && scope.state.widget.widget is TWidget,
    "Could not find $TWidget. Please define $TWidget in the element above.",
  );
  return scope!.state.widget.widget as TWidget;
}