Skip to main content

prefer-any-or-every

added in: 1.12.0
🛠
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

collection
.where((item) => item.isEven)
.isEmpty; // LINT
}

✅ Good:

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

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

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