跳转至主要内容

non_exhaustive_switch_statement

The type '{0}' is not exhaustively matched by the switch cases since it doesn't match '{1}'.

Description

#

The analyzer produces this diagnostic when a switch statement switching over an exhaustive type is missing a case for one or more of the possible values that could flow through it.

Example

#

The following code produces this diagnostic because the switch statement doesn't have a case for the value E.three, and E is an exhaustive type:

dart
enum E { one, two, three }

void f(E e) {
  switch (e) {
    case E.one:
    case E.two:
  }
}

Common fixes

#

Add a case for each of the constants that aren't currently being matched:

dart
enum E { one, two, three }

void f(E e) {
  switch (e) {
    case E.one:
    case E.two:
      break;
    case E.three:
  }
}

If the missing values don't need to be matched, then add a default clause or a wildcard pattern:

dart
enum E { one, two, three }

void f(E e) {
  switch (e) {
    case E.one:
    case E.two:
      break;
    default:
  }
}