pattern_assignment_not_local_variable
Only local variables can be assigned in pattern assignments.
Description
#The analyzer produces this diagnostic when a pattern assignment assigns a value to anything other than a local variable. Patterns can't assign to fields or top-level variables.
Example
#If the code is cleaner when destructuring with a pattern, then rewrite the code to assign the value to a local variable in a pattern declaration, assigning the non-local variable separately:
class C {
var x = 0;
void f((int, int) r) {
(x, _) = r;
}
}
Common fixes
#If the code is cleaner when using a pattern assignment, then rewrite the code to assign the value to a local variable, assigning the non-local variable separately:
class C {
var x = 0;
void f((int, int) r) {
var (a, _) = r;
x = a;
}
}
If the code is cleaner without using a pattern assignment, then rewrite the code to not use a pattern assignment:
class C {
var x = 0;
void f((int, int) r) {
x = r.$1;
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。