avoid_unnecessary_constructor
v1.0.0 Warning Code Organization
Flags an empty unnamed constructor that writes out exactly what Dart supplies when no constructor is declared at all.
class A { A(); } has no parameters, no initializers, no body and no documentation. The line adds nothing a reader can act on, and it invites the assumption that construction does something.
This rule is in the opinionated preset, so it is on with preset: opinionated or preset: pedantic.
class UserRepository { UserRepository();
Future<void> refresh() async {}}class UserRepository { Future<void> refresh() async {}}Examples
Section titled “Examples”const is a real difference
Section titled “const is a real difference”const A(); lets callers write const A(), which the implicit constructor does not allow. It is never reported:
// Accepted — the const-ness is the pointclass EmptyState { const EmptyState();}
void render() { const state = EmptyState();}A second constructor makes the empty one load-bearing
Section titled “A second constructor makes the empty one load-bearing”Dart supplies the unnamed constructor only when no constructor is written. Once a named one exists, deleting A() makes A() illegal:
// Accepted — removing `Duration()` would break every plain `Duration()` callclass Timeout { Timeout();
Timeout.seconds(this.value);
int value = 0;}Documentation and annotations carry information
Section titled “Documentation and annotations carry information”An empty constructor that is documented or annotated says something the implicit one cannot, so it stays:
// Acceptedclass Analytics { /// Constructing this is cheap; the transport connects lazily. Analytics();}// Acceptedclass Legacy { @Deprecated('Use Legacy.fromConfig instead.') Legacy();}Named and private constructors are a different construct
Section titled “Named and private constructors are a different construct”Only the unnamed one duplicates a default:
// Accepted — there is no implicit `Cache._()`class Cache { Cache._();
static final instance = Cache._();}Known limitations
Section titled “Known limitations”No quick fix. Deleting the line is safe in the reported cases, but the constructor is often the anchor a diff or a code review is hanging on, and removing it silently would hide that.
Turning this rule off
Section titled “Turning this rule off”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.