メインコンテンツにスキップ

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;
}