Skip to main content

avoid-duplicate-field-initializers

effort: 3m
pro+

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

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

Example

❌ Bad:

class Some {
final value = 'hello';
// LINT: This field has the same initializer as 'value'.
// Try changing the initializer or reusing the existing field.
final another = 'hello';
}

class Primary(String val) {
final another = val;
// LINT: This field has the same initializer as 'another'.
// Try changing the initializer or reusing the existing field.
final third = val;

final computed = val + val;
// LINT: This field has the same initializer as 'computed'.
// Try changing the initializer or reusing the existing field.
final anotherComputed = val + val;
}

✅ Good:

class Some {
final value = 'hello';
final another = 'world';
}

class Primary(String val) {
final another = val;

final computed = val + val;
}

Additional Resources