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.
Why use this rule
Section titled “Why use this rule”.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 | Dart lint: prefer_iterable_whereType
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); }}Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
prefer_any_or_every: true.
To turn it off:
rules: prefer_any_or_every: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_accessing_collections_by_constant_index— Avoid accessing a collection by a constant index inside a loop.avoid_collection_equality_checks— Avoid comparing collections with == or != as it checks reference equality, not contents.avoid_collection_methods_with_unrelated_types— Avoid calling collection methods with arguments whose types are unrelated to the collection’s type parameter.avoid_duplicate_collection_elements— Don’t repeat the same element in a collection literal.