avoid-duplicate-constant-values
Warns when a class or enum declaration has several constants with the same primitive values.
Duplicate enum constants are usually the result of a typo and a sign of a bug.
⚙️ Config
Set allowed
(default is [-1, 0, 1]
) to configure the list of allowed constants.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-duplicate-constant-values:
allowed: [1, "value"]
Example
❌ Bad:
enum MyEnum {
a('hi'),
// LINT: Avoid duplicate constant values.
// Try changing this value or moving it out to a separate constant.
b('hi'),
c('another');
final String value;
const MyEnum(this.value);
}
class RuleType {
final String value;
const RuleType._(this.value);
static const common = RuleType._('common');
// LINT: Avoid duplicate constant values.
// Try changing this value or moving it out to a separate constant.
static const flutter = RuleType._('common');
}
✅ Good:
enum MyEnum {
a('hi'),
b('hello'), // Correct, different value
c('another');
final String value;
const MyEnum(this.value);
}
class RuleType {
final String value;
const RuleType._(this.value);
static const common = RuleType._('common');
static const flutter = RuleType._('flutter'); // Correct, different value
}