跳到主要內容

prefer_if_null_operators

穩定
建議
提供修正

偏好使用 ?? 運算子。

詳細資訊

#

建議 使用 ?? 運算子,而不是 null 檢查和條件運算式。

不良示範

dart
v = a == null ? b : a;

良好示範

dart
v = a ?? b;

啟用

#

若要啟用 prefer_if_null_operators 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 prefer_if_null_operators

analysis_options.yaml
yaml
linter:
  rules:
    - prefer_if_null_operators

如果您改為使用 YAML map 語法來設定 linter 規則,請在 linter > rules 下新增 prefer_if_null_operators: true

analysis_options.yaml
yaml
linter:
  rules:
    prefer_if_null_operators: true