Skip to content

avoid_unnecessary_constructor

v1.0.0 Warning Code Organization

This rule flags an empty unnamed constructor that matches the one Dart provides when no constructor is declared at all.

class A { A(); } writes out exactly the default: no parameters, no initializers, no body, no documentation. The line adds nothing a reader can act on, and it invites the assumption that construction does something.

Several shapes are deliberately not reported, because each does something the implicit constructor cannot:

  • const A(); — lets callers write const A().
  • A named or private constructor.
  • A documented or annotated one, which carries information even when empty.
  • Any class with a second constructor: Dart only supplies the unnamed one when no constructor is written, so there the empty A() is what keeps A() legal.
class Repository {
Repository();
}
class Repository {}

This rule is in the opinionated preset.

To disable this rule:

many_lints.yaml
rules:
avoid_unnecessary_constructor: false

To keep the rule on but skip certain paths, use per-rule exclude.