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:
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:
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:
void f(int x) {
if (x case var a when 1 > 0) {
print(a);
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.