anyContains method

  1. @useResult
bool anyContains(
  1. String? check, {
  2. bool caseSensitive = true,
})

Returns true if any element contains check as a substring.

A null or empty check, or an empty list, returns false (there is no meaningful substring to look for). When caseSensitive is false both sides are lowercased with the invariant culture before comparing.

Example:

['HelloWorld'].anyContains('world', caseSensitive: false); // true
['HelloWorld'].anyContains('world', caseSensitive: true);  // false
<String>[].anyContains('x');                               // false

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
bool anyContains(String? check, {bool caseSensitive = true}) {
  if (isEmpty) {
    return false;
  }

  // A null/empty needle has no substring to match; short-circuit before any
  // per-element scan so a 1M-element list still returns immediately.
  if (check == null || check.isEmpty) {
    return false;
  }

  if (caseSensitive) {
    return any((String e) => e.contains(check));
  }

  // Lowercase the needle once outside the scan rather than per element.
  final String checkLower = check.toLowerCase();
  return any((String e) => e.toLowerCase().contains(checkLower));
}