prefer_if_null_operators
Use the '??' operator rather than '?:' when testing for 'null'.
Description
#The analyzer produces this diagnostic when a conditional expression (using
the ?:
operator) is used to select a different value when a local
variable is null
.
Example
#The following code produces this diagnostic because the variable s
is
being compared to null
so that a different value can be returned when
s
is null
:
dart
String f(String? s) => s == null ? '' : s;
Common fixes
#Use the if-null operator instead:
dart
String f(String? s) => s ?? '';
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。