const_constructor_with_non_final_field
不能為帶有非 final 欄位的類定義 const 建構函式。
描述
#當建構函式被標記為 const 建構函式,但該建構函式定義的類至少有一個非 final 例項欄位(直接或繼承)時,分析器會產生此診斷資訊。
示例
#以下程式碼會產生此診斷資訊,因為欄位 x 不是 final 的
dart
class C {
int x;
const C(this.x);
}常見修復
#如果可以將所有欄位標記為 final,請這樣做
dart
class C {
final int x;
const C(this.x);
}如果不能將所有欄位標記為 final,請從建構函式中移除 const 關鍵字
dart
class C {
int x;
C(this.x);
}