Skip to content

duplicate_enum_constant

Reports an enum constant made exactly as an earlier one, where the other constants all differ. Give it the arguments it was meant to have.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

A constant of an enum made exactly as an earlier constant of the same enum: the same constructor, the same type arguments, and the same arguments written as the same code (each positional one at its place, each named one by its name, in any order). The violation is at the later constant, and the message names the earlier one.

It is reported only where the arguments otherwise tell the constants apart: the enum has three constants or more, and this is the one pair of them made alike.

It does not report:

  • constants without arguments;
  • an enum of two constants;
  • constants that differ in an argument, the constructor or a type argument;
  • values equal but written differently, such as 8 and 4 + 4;
  • an enum whose arguments repeat more than once, as levels, sizes or flags that constants share by design.

Two such constants differ only in their name and their index. Every field, getter and method gives the same for both, and a lookup by a field, such as values.firstWhere((e) => e.path == path), never finds the later one. Where every other constant has arguments of its own, the one repeat is most likely a copy that was not changed.

Violating
enum Endpoint {
users('/users'),
posts('/users'),
tags('/tags');
const Endpoint(this.path);
final String path;
}
Corrected
enum Endpoint {
users('/users'),
posts('/posts'),
tags('/tags');
const Endpoint(this.path);
final String path;
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// The old name stays as an alias until the next major release.
// ignore: assay/duplicate_enum_constant

// ignore_for_file: assay/duplicate_enum_constant silences it in a whole file, and // ignore: duplicate_enum_constant without the prefix works too. See Suppressions.