跳到主要内容

prefer_if_null_operators

稳定
推荐
可用修复

首选使用 ?? 运算符。

详情

#

首选使用 ?? 运算符,而非空检查和条件表达式。

不推荐

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