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;
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.