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
:
int? f(List<int>? p) {
return p == null ? null : p.length;
}
Common fixes
#Use a null-aware access operator instead:
int? f(List<int>? p) {
return p?.length;
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.