Skip to main content

avoid-wildcard-cases-with-enums

added in: 1.10.0
Dart 3.0+
Pro+
preset: recommended

Warns when a switch on the Enum value has a wildcard pattern case.

Once a new constant is added to a Enum, the analyzer will highlight all cases where this new constant should be used, unless there is a wildcard case.

Example

❌ Bad:

enum MyEnum { first, second }

void someFn() {
final value = MyEnum.first;

final result = switch (value) {
MyEnum.first => 1,
_ => 2, // LINT: Avoid wildcard cases with 'Enums'. Try enumerating all the possible values of the switch expression.
};
}

✅ Good:

enum MyEnum { first, second }

void someFn() {
final value = MyEnum.first;

final result = switch (value) {
MyEnum.first => 1,
MyEnum.second => 2, // Correct, all enum constants are listed
};
}