any method

MultiWidgetMatcher<W> any(
  1. void matcher(
    1. WidgetMatcher<W>
    )
)

Asserts that at least one of the matched widgets fulfills the provided matcher.

If none of the matched widgets fulfill the matcher, a TestFailure is thrown. This method is useful for cases when at least one widget is expected to meet a certain condition.

Implementation

MultiWidgetMatcher<W> any(void Function(WidgetMatcher<W>) matcher) {
  TestAsyncUtils.guardSync();
  if (discovered.isEmpty) {
    throw Exception('Expected at least one match for $this, but found none');
  }

  late String matcherDescription;
  final found = discovered.any((wm) {
    try {
      matcher(wm);
      return true;
    } catch (e) {
      matcherDescription =
          e is PropertyCheckFailure ? e.matcherDescription : e.toString();
      return false;
    }
  });

  if (found) {
    return this;
  }
  throw TestFailure(
    "Expected that at least one candidate fulfills matcher '$matcherDescription', but none did.",
  );
}