Skip to main content

use_null_aware_elements

Stable
Fix available

If-elements testing for null can be replaced with null-aware elements.

Details

#

Where possible, use null-aware elements in collection literals.

BAD:

dart
f(String? key) => {if (key != null) key: "value"};

GOOD:

dart
f(String? key) => {?key: "value"};

Enable

#

To enable the use_null_aware_elements rule, add use_null_aware_elements under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - use_null_aware_elements

If you're instead using the YAML map syntax to configure linter rules, add use_null_aware_elements: true under linter > rules:

analysis_options.yaml
yaml
linter:
  rules:
    use_null_aware_elements: true