Skip to content

prefer_any_or_every

v0.1.0 Warning Fix Collection & Type

Using .where(predicate).isNotEmpty can be replaced with .any(predicate), and .where(predicate).isEmpty can be replaced with .every(negatedPredicate). The dedicated methods are more readable and can short-circuit evaluation, avoiding the creation of an intermediate lazy iterable.

.any() and .every() express intent more clearly and stop iterating as soon as the result is determined. .where() creates an intermediate Iterable that is unnecessary when you only need a boolean check.

See also: Iterable.any | Iterable.every

class Example {
final List<int> numbers = [1, 2, 3, 4, 5];
void checkNumbers() {
// Use .any() instead of .where().isNotEmpty
final hasEven = numbers.where((n) => n.isEven).isNotEmpty;
// Use .every() instead of .where().isEmpty
final allPositive = numbers.where((n) => n < 0).isEmpty;
}
}
class Example {
final List<int> numbers = [1, 2, 3, 4, 5];
void checkNumbers() {
final hasEven = numbers.any((n) => n.isEven);
final allPositive = numbers.every((n) => n >= 0);
}
}

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_any_or_every: false