Skip to content

prefer_declaring_const_constructor

v1.0.0WarningCode Quality

This rule flags a class that could declare a const constructor but does not.

A const constructor is what lets a value be built once at compile time and shared, rather than allocated at every call. In Flutter that is the difference between a widget the framework can skip rebuilding and one it cannot — which is why prefer_const_constructors is in every lint preset. But that rule only fires where a const constructor already exists; this one asks for the constructor in the first place.

A class qualifies when every field is final, it declares exactly one generative constructor, that constructor’s body is empty, its superclass offers a const constructor to chain to, and every initializer is itself const-evaluable.

That last check matters more than it looks: _random = random ?? Random.secure() is a field initializer whose value is a call, so suggesting const for it would produce code that does not build. Both of the rule’s initial hits on a production codebase were exactly that shape.

Classes marked @immutable are skipped, since the SDK’s prefer_const_constructors_in_immutables already covers them.

This rule is in the pedantic preset.

class Point {
final int x;
final int y;
Point(this.x, this.y);
}
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
prefer_declaring_const_constructor:
enabled: true

To disable this rule:

many_lints.yaml
rules:
prefer_declaring_const_constructor: false

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