prefer_null_aware_operators
Use the null-aware operator '?.' rather than an explicit 'null' comparison.
Description
#The analyzer produces this diagnostic when a comparison with null
is
used to guard a member reference, and null
is used as a result when the
guarded target is null
.
Example
#The following code produces this diagnostic because the invocation of
length
is guarded by a null
comparison even though the default value
is null
:
dart
int? f(List<int>? p) {
return p == null ? null : p.length;
}
Common fixes
#Use a null-aware access operator instead:
dart
int? f(List<int>? p) {
return p?.length;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。