pattern_variable_assignment_inside_guard
Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
Description
#The analyzer produces this diagnostic when a pattern variable is assigned a value inside a guard (when
) clause.
Example
#The following code produces this diagnostic because the variable a
is assigned a value inside the guard clause:
dart
void f(int x) {
if (x case var a when (a = 1) > 0) {
print(a);
}
}
Common fixes
#If there's a value you need to capture, then assign it to a different variable:
dart
void f(int x) {
var b;
if (x case var a when (b = 1) > 0) {
print(a + b);
}
}
If there isn't a value you need to capture, then remove the assignment:
dart
void f(int x) {
if (x case var a when 1 > 0) {
print(a);
}
}
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.