Skip to main content

avoid-non-empty-constructor-bodies

added in: 1.23.0
Pro+

Suggests moving all initialization logic to a separate method instead of the constructor.

Use this rule if you have specific classes that require the initialization rule to be moved out to a separate initialization method.

note

For this rule to work only for specific classes, use the include config.

Example

❌ Bad:

class SomeClass {
final String value;

// LINT: Avoid non-empty constructor bodies. Try moving all initialization logic to a separate method.
SomeClass(this.value) {
log(this.value);
}
}

✅ Good:

class SomeClass {
final String value;

const SomeClass(this.value);

void init() {
log(this.value);
}
}