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:
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:
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:
void f(int e, int i) {
if (e == i) {}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.