Skip to main content

prefer-any-or-every

added in: 1.12.0
🛠
Pro+
preset: recommended

Warns when a collection's call chain can be rewritten to use .any() or .every() instead of .isEmpty or .isNotEmpty.

Using .any() or .every() helps to abort the calculation when the condition is true (or false) the first time, resulting in more performant code.

Example

❌ Bad:

void fn() {
final collection = [1, 2, 3, 4, 5];

collection
.where((item) => item.isEven)
.isNotEmpty; // LINT: Prefer '.any()' instead of 'where().isNotEmpty'.

collection
.where((item) => item.isEven)
.isEmpty; // LINT: Prefer '.every()' instead of 'where().isEmpty'.
}

✅ Good:

void fn() {
final collection = [1, 2, 3, 4, 5];

collection.any((item) => item.isEven);

collection.every((item) => !item.isEven);
}