prefer-contains
preset: recommended
Suggests using .contains
instead of .indexOf
when checking for the presence of an element.
Example
❌ Bad:
void fn() {
final list = [1, 2, 3];
if (list.indexOf(1) == -1) {} // LINT: Prefer '.contains' instead of '.indexOf' when checking for the presence of an element.
if (list.indexOf(1) != -1) {} // LINT: Prefer '.contains' instead of '.indexOf' when checking for the presence of an element.
}
✅ Good:
void fn() {
final list = [1, 2, 3];
if (list.indexOf(1) == 2) {}
if (!list.contains(1)) {}
if (list.contains(1)) {}
}