findRootAncestorStateOfType<T extends State<StatefulWidget>> method

  1. @override
T? findRootAncestorStateOfType<T extends State<StatefulWidget>>()
override

Returns the State object of the furthest ancestor StatefulWidget widget that is an instance of the given type T.

Implementation

@override
T? findRootAncestorStateOfType<T extends State<StatefulWidget>>() {
  RenderElement? ancestor = _parent;

  StatefulRenderElement? statefulAncestor;

  while (ancestor != null) {
    if (ancestor is StatefulRenderElement && ancestor.state is T) {
      statefulAncestor = ancestor;
    }

    ancestor = ancestor._parent;
  }

  return statefulAncestor?.state as T?;
}