visitElement<T extends Element> method

T visitElement<T extends Element>({
  1. bool last = false,
  2. bool filter(
    1. T element
    )?,
  3. String? assertType,
})

Returns the first T with filter below this BuildContext.

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

Implementation

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