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:
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:
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:
void f((int, int) r) {
if (r case (var a, 0) && (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.