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);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。