跳到主要内容

use_null_aware_elements

稳定
可用修复

测试 null 的 if 元素可以替换为 null 感知元素。

详情

#

在可能的情况下,在集合字面量中使用 null 感知元素。

BAD

dart
f(String? key) => {if (key != null) key: "value"};

GOOD

dart
f(String? key) => {?key: "value"};

启用

#

要启用 use_null_aware_elements 规则,请在 analysis_options.yaml 文件中的 linter > rules 下添加 use_null_aware_elements

analysis_options.yaml
yaml
linter:
  rules:
    - use_null_aware_elements

如果您改为使用 YAML 映射语法配置 Linter 规则,请在 linter > rules 下添加 use_null_aware_elements: true

analysis_options.yaml
yaml
linter:
  rules:
    use_null_aware_elements: true