prefer_const_constructors
Prefer const with constant constructors.
This rule is available as of Dart 2.0.0.
Rule sets: flutter
This rule has a quick fix available.
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);
Usage
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