Skip to main content

avoid-enum-values-by-index

Warns when a enum value is accessed via Enum.values[index].

Accessing enum values by index is unstable and can lead to tricky bugs because the order of the enum values can change in the future (for example, when a new value is added).

Example

❌ Bad:

enum SomeEnums { first, second }

void fn() {
// LINT: Avoid accessing enum values by index. Try using '.firstWhere' or '.byName' instead.
SomeEnums.values[0];
}

✅ Good:

void fn() {
SomeEnums.values.byName('first');
}