prefer_declaring_const_constructor
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);}Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: prefer_declaring_const_constructor: enabled: trueTurning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_declaring_const_constructor: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_accessing_other_classes_private_members— Make the underscore mean what everyone reads it as.avoid_commented_out_code— Detect and flag commented-out code.avoid_complex_conditions— Keep boolean conditions within an operand budget.avoid_deep_nesting— Keep control flow within a nesting budget.