跳转至主要内容

non_exhaustive_switch_expression

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 expression 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 expression doesn't have a case for the value E.three:

dart
enum E { one, two, three }

String f(E e) => switch (e) {
    E.one => 'one',
    E.two => 'two',
  };

Common fixes

#

If the missing values are distinctly meaningful to the switch expression, then add a case for each of the values missing a match:

dart
enum E { one, two, three }

String f(E e) => switch (e) {
    E.one => 'one',
    E.two => 'two',
    E.three => 'three',
  };

If the missing values don't need to be matched, then add a wildcard pattern that returns a simple default:

dart
enum E { one, two, three }

String f(E e) => switch (e) {
    E.one => 'one',
    E.two => 'two',
    _ => 'unknown',
  };