avoid-unnecessary-reassignment
preset: recommended
Warns when a value is reassigned to a variable without using the initial value.
Example
❌ Bad:
String alpha = '';
if (something) {
alpha = 'true value'; // LINT: Avoid unnecessary reassignment. Try moving this assignment to the initial variable declaration.
} else {
alpha = 'false value'; // LINT: Avoid unnecessary reassignment. Try moving this assignment to the initial variable declaration.
}
String beta = '';
beta = something ? 'true value' : 'false value'; // LINT: Avoid unnecessary reassignment. Try moving this assignment to the initial variable declaration.
SomeObject object = SomeObject();
object = something ? SomeObject() : SomeObject(); // LINT: Avoid unnecessary reassignment. Try moving this assignment to the initial variable declaration.
✅ Good:
final alpha = something ? 'true value' : 'false value';
final beta = something ? 'true value' : 'false value';
final object = SomeObject();