Skip to main content

avoid-duplicate-initializers

Warns when a final variable has the same initializer as another variable in scope.

Variables with duplicate initializers are either the result of a typo or are redundant and can be simply removed.

Example

❌ Bad:

void fn(SomeClass param) {
final myValue = param.value.isNotEmpty;

// LINT: This variable has the same initializer as 'myValue'.
// Try changing the initializer or reusing the existing variable.
final anotherValue = param.value.isNotEmpty;
}

✅ Good:

void fn(SomeClass param) {
final myValue = param.value.isNotEmpty;
final anotherValue = param.anotherValue; // Correct, different initializer
}

Known Limitations

This rule cannot check if the state of an object has changed after a method invocation and can falsy lint duplicate (but actually different) declarations. Consider adding an // ignore: comment with a short description for any future reader.

final oldValue = object.property;
object.update();
// ignore: avoid-duplicate-initializers, "update" changes the state
final newValue = object.property;
if (oldValue != newValue) {
...
}

Additional Resources