跳到主要內容

use_if_null_to_convert_nulls_to_bools

使用 if-null 運算子將 'null' 轉換為 'bool'。

描述

#

當可為空的 bool 型別表示式與布林文字進行比較(使用 ==!=)時,分析器會產生此診斷訊息。

示例

#

以下程式碼會產生此診斷訊息,因為可為空的布林變數 btrue 進行了比較。

dart
void f(bool? b) {
  if (b == true) {
    // Treats `null` as `false`.
  }
}

常見修復

#

將條件重寫為使用 ?? 代替。

dart
void f(bool? b) {
  if (b ?? false) {
    // Treats `null` as `false`.
  }
}