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) {}
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.