visitState<T extends State<StatefulWidget>> method

T visitState<T extends State<StatefulWidget>>({
  1. bool last = false,
  2. bool filter(
    1. T state
    )?,
  3. String? assertType,
})

Returns the first T with filter below this BuildContext.

If last is true, then it will return the last State found. Visiting last is O(N), avoid using last = true.

The assertType, replaces T on assert messages.

Implementation

T visitState<T extends State>({
  bool last = false,
  bool Function(T state)? filter,
  String? assertType,
}) {
  final list = <T>[];
  final state = visitStateOrNull<T>(
    last: last,
    filter: (state) {
      list.add(state);
      return filter?.call(state) ?? true;
    },
  );
  final extra = list.isNotEmpty ? 'Found $list.' : '';
  assert(state != null, 'No ${assertType ?? T} at this context. $extra');
  return state!;
}