Dart Analyze logoDart Analyze/
DRT-W1314

Duplicate field or getter in pattern matchingDRT-W1314

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a record pattern matches the same field more than once, or when an object pattern matches the same getter more than once.

Examples

The following code produces this diagnostic because the record field a is matched twice in the same record pattern:

void f(({int a, int b}) r) {
  switch (r) {
    case (a: 1, a: 2):
      return;
  }
}

The following code produces this diagnostic because the getter f is matched twice in the same object pattern:

void f(Object o) {
  switch (o) {
    case C(f: 1, f: 2):
      return;
  }
}
class C {
  int? f;
}

Common fixes

If the pattern should match for more than one value of the duplicated field, then use a logical-or pattern:

void f(({int a, int b}) r) {
  switch (r) {
    case (a: 1, b: _) || (a: 2, b: _):
      break;
  }
}

If the pattern should match against multiple fields, then change the name of one of the fields:

void f(({int a, int b}) r) {
  switch (r) {
    case (a: 1, b: 2):
      return;
  }
}