Skip to main content

prefer-contains

added in: 1.22.0
🛠
Pro+

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) == 2) {}
if (list.indexOf(1) == -1) {} // LINT
if (list.indexOf(1) != -1) {} // LINT
}

✅ Good:

void fn() {
final list = [1, 2, 3];

if (list.indexOf(1) == 2) {}
if (!list.contains(1)) {}
if (list.contains(1)) {}
}