prefer_constructors_over_static_methods
靜態方法應為建構函式。
描述
#分析器會在靜態方法返回新建立的類例項,因此可以被替換為建構函式時,生成此診斷資訊。
示例
#以下程式碼會生成此診斷資訊,因為靜態方法 all 可以是建構函式
dart
class C {
final int a, b, c;
C(this.a, this.b, this.c);
static C all(int i) => C(i, i, i);
}常見修復方法
#將靜態方法轉換為命名建構函式
dart
class C {
final int a, b, c;
C(this.a, this.b, this.c);
C.all(int i) : a = i, b = i, c = i;
}