Skip to main content

prefer-contains

added in: 1.22.0
🛠
Pro+
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)) {}
}