Skip to content

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 {}
}

const A(); lets callers write const A(), which the implicit constructor does not allow. It is never reported:

// Accepted — the const-ness is the point
class 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()` call
class 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:

// Accepted
class Analytics {
/// Constructing this is cheap; the transport connects lazily.
Analytics();
}
// Accepted
class 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._();
}

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.

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.