Skip to main content

avoid-duplicate-constant-values

added in: 1.22.0
⚙️
Pro+

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'),
b('hi'), // LINT: Avoid duplicate constant values. Try changing this value or moving it out to a separate constant.
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._('common'); // LINT: Avoid duplicate constant values. Try changing this value or moving it out to a separate constant.
}

✅ 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
}

Additional Resources