constant_pattern_with_non_constant_expression
The expression of a constant pattern must be a valid constant.
Description
#The analyzer produces this diagnostic when a constant pattern has an expression that isn't a valid constant.
Example
#The following code produces this diagnostic because the constant pattern
i
isn't a constant:
dart
void f(int e, int i) {
switch (e) {
case i:
break;
}
}
Common fixes
#If the value that should be matched is known, then replace the expression with a constant:
dart
void f(int e, int i) {
switch (e) {
case 0:
break;
}
}
If the value that should be matched isn't known, then rewrite the code to not use a pattern:
dart
void f(int e, int i) {
if (e == i) {}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。