跳到主要内容

unnecessary_null_in_if_null_operators

不必要地在 '??' 运算符中使用 'null'。

描述

#

?? 运算符的右侧操作数为字面量 null 时,分析器会产生此诊断信息。

示例

#

以下代码会产生此诊断信息,因为 ?? 运算符的右侧操作数是 null

dart
String? f(String? s) => s ?? null;

常见修复

#

如果右侧操作数应使用非空值,则更改右侧。

dart
String f(String? s) => s ?? '';

如果右侧操作数没有非空值可用,则移除该运算符和右侧操作数。

dart
String? f(String? s) => s;