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',
};
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。