use_null_aware_elements
Use the null-aware marker '?' rather than a null check via an 'if'.
Description
#The analyzer produces this diagnostic when a null check is used instead of a null-aware marker inside of a collection literal.
Example
#The following code produces this diagnostic because a null check is used to decide whether x
should be inserted into the list, while the null-aware marker '?' would be less brittle and less verbose.
dart
f(int? x) => [if (x != null) x];
Common fixes
#Replace the null-check with the null-aware marker '?':
dart
f(int? x) => [?x];
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.