prefer_ const_ constructors
Learn about the prefer_const_constructors linter rule.
Prefer const with constant constructors.
Details
#PREFER using const for instantiating constant constructors.
If a constructor can be invoked as const to produce a canonicalized instance, it's preferable to do so.
BAD:
class A { const A(); } void accessA() { A a = new A(); } GOOD:
class A { const A(); } void accessA() { A a = const A(); } GOOD:
class A { final int x; const A(this.x); } A foo(int x) => new A(x);
Enable
# To enable the prefer_const_constructors rule, add prefer_const_constructors under linter > rules in your analysis_options.yaml file:
linter: rules: - prefer_const_constructors If you're instead using the YAML map syntax to configure linter rules, add prefer_const_constructors: true under linter > rules:
linter: rules: prefer_const_constructors: true Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Report an issue.