跳到主要內容

tighten_type_of_initializing_formals

使用型別註解而不是 'assert' 來強制執行非空性。

描述

#

分析器會在建構函式的初始化列表中使用 assert 來確保只使用非 null 值初始化欄位時產生此診斷資訊。

示例

#

以下程式碼產生此診斷資訊,因為正在使用 assert 來捕獲一個本可以由型別系統捕獲的錯誤。

dart
class C {
  final String? s;

  C(this.s) : assert(s != null);
}

常見修復方法

#

移除 assert 並在初始化形參前新增非空型別。

dart
class C {
  final String? s;

  C(String this.s);
}