prefer_const_constructors
对于常量的构造函数,优先使用 const
。
详情
#优先对常量构造函数的实例化使用 const
。
如果构造函数可以作为 const 调用以生成规范化实例,则优先这样做。
不好
dart
class A {
const A();
}
void accessA() {
A a = new A();
}
好
dart
class A {
const A();
}
void accessA() {
A a = const A();
}
好
dart
class A {
final int x;
const A(this.x);
}
A foo(int x) => new A(x);
启用
#要启用 prefer_const_constructors
规则,请在你的 analysis_options.yaml
文件中的 linter > rules 下添加 prefer_const_constructors
analysis_options.yaml
yaml
linter:
rules:
- prefer_const_constructors
如果你使用的是 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 prefer_const_constructors: true
analysis_options.yaml
yaml
linter:
rules:
prefer_const_constructors: true