跳到主要內容

use_null_aware_elements

使用 null-aware 標記 '?',而不是透過 'if' 進行 null 檢查。

描述

#

當在集合字面量中使用了 null 檢查而不是 null-aware 標記時,分析器會生成此診斷。

示例

#

以下程式碼會生成此診斷,因為它使用 null 檢查來決定是否應將 x 插入列表中,而 null-aware 標記 '?' 將會更簡潔且不易出錯。

dart
f(int? x) => [if (x != null) x];

常見修復

#

用 null-aware 標記 '?' 替換 null 檢查

dart
f(int? x) => [?x];