duplicate_variable_pattern
The variable '{0}' is already defined in this pattern.
Description
#The analyzer produces this diagnostic when a branch of a logical-and pattern declares a variable that is already declared in an earlier branch of the same pattern.
Example
#The following code produces this diagnostic because the variable a
is
declared in both branches of the logical-and pattern:
dart
void f((int, int) r) {
if (r case (var a, 0) && (0, var a)) {
print(a);
}
}
Common fixes
#If you need to capture the matched value in multiple branches, then change the names of the variables so that they are unique:
dart
void f((int, int) r) {
if (r case (var a, 0) && (0, var b)) {
print(a + b);
}
}
If you only need to capture the matched value on one branch, then remove the variable pattern from all but one branch:
dart
void f((int, int) r) {
if (r case (var a, 0) && (0, _)) {
print(a);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。