Skip to main content

prefer-contains

has auto-fix
pro+

Suggests using .contains instead of .indexOf when checking for the presence of an element.

Example

❌ Bad:

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

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

✅ Good:

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

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