state<StateType extends State<StatefulWidget>> method

StateType? state<StateType extends State<StatefulWidget>>([
  1. Finder? subtreeScope
])

Finds StatefulElements whose State is of type StateType, optionally scoped to the given subtreeScope, and returns the element's associated StateType object.

This method expects to find, at most, one StateType object.

Example - assume a widget MyWidget with a state MyWidgetState:

final state = find.state<MyWidgetState>();
state?.myCustomStateMethod();

Implementation

StateType? state<StateType extends State>([Finder? subtreeScope]) {
  final elementFinder =
      find.byElementPredicate((element) => element is StatefulElement && element.state is StateType);
  final Finder stateFinder =
      subtreeScope != null ? find.descendant(of: subtreeScope, matching: elementFinder) : elementFinder;

  final finderResult = stateFinder.evaluate();
  if (finderResult.length > 1) {
    throw Exception("Expected to find no more than one $StateType, but found ${finderResult.length}");
  }
  if (finderResult.isEmpty) {
    return null;
  }

  final foundElement = stateFinder.evaluate().single as StatefulElement;
  return foundElement.state as StateType;
}