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.
Why use this rule
Section titled “Why use this rule”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 writeconst 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 keepsA()legal.
class Repository { Repository();}class Repository {}Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset.
To disable this rule:
rules: avoid_unnecessary_constructor: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”arguments_ordering— Keep named arguments in a configured order.avoid_duplicate_mixins— Flag a mixin applied twice in onewithclause.avoid_generics_shadowing— Avoid generic type parameters that shadow top-level declarations.avoid_unnecessary_extends— Remove an explicitextends Object.