Skip to main content

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;
}