prefer-primary-constructors
Suggests converting simple classes (with only fields) to primary constructors.
⚙️ Config
Set allow-getters (default is false) to also highlight classes with getters.
analysis_options.yaml
dcm:
rules:
- prefer-primary-constructors:
allow-getters: false
Example
❌ Bad:
class MyClass {
final String value;
final int another;
// LINT: Prefer primary constructors for simple classes.
const new(this.another, this.value);
}
class WithStaticField {
static const _field = 'value';
final String value;
final int another;
// LINT: Prefer primary constructors for simple classes.
const new(this.another, this.value);
}
✅ Good:
class Some {
String? value;
int? another;
}
class Another {
final String value;
final int another;
const new(this.another, this.value);
new named(this.another, this.value);
}
class WithMethods {
final String value;
final int another;
const new(this.another, this.value);
void work() {}
}