prefer_const_constructors
偏好使用 const
與常數建構函式。
此規則自 Dart 2.0 起提供。
此規則具有可用的快速修正。
詳細資訊
#偏好使用 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
除非另有說明,否則本網站的文件反映的是 Dart 3.6.0。頁面最後更新於 2024-07-03。 檢視原始碼 或 回報問題。