Skip to main content

no_duplicate_case_values

The value of the case clause ('{0}') is equal to the value of an earlier case clause ('{1}').

Description

#

The analyzer produces this diagnostic when two or more case clauses in the same switch statement have the same value.

Any case clauses after the first can't be executed, so having duplicate case clauses is misleading.

This diagnostic is often the result of either a typo or a change to the value of a constant.

Example

#

The following code produces this diagnostic because two case clauses have the same value (1):

dart
// @dart = 2.14
void f(int v) {
  switch (v) {
    case 1:
      break;
    case 1:
      break;
  }
}

Common fixes

#

If one of the clauses should have a different value, then change the value of the clause:

dart
void f(int v) {
  switch (v) {
    case 1:
      break;
    case 2:
      break;
  }
}

If the value is correct, then merge the statements into a single clause:

dart
void f(int v) {
  switch (v) {
    case 1:
      break;
  }
}