Skip to main content

use-existing-variable

added in: 1.23.0
🛠
Pro+

Warns when an expression can be replaced by an existing variable with the same initializer.

Reusing already declared variables helps avoid inconsistencies when only one of the repeated expressions is updated.

Example

❌ Bad:

void fn(String value) {
final some = value.length.isOdd;

print(value.length.isOdd); // LINT: This expression can be replaced with an existing variable 'some' that has the same initializer.
}

✅ Good:

void fn(String value) {
final some = value.length.isOdd;

print(some); // Correct, reuses existing variable
}