Skip to main content

prefer-null-aware-spread

added in: 1.11.0
Pro+
preset: recommended

Warns when a null check inside a collection literal can be replaced with a null-aware spread (...?).

Example

❌ Bad:

void fn() {
final Set<String>? localSet = <String>{};

final collection = [
if (localSet != null) ...localSet, // LINT: Prefer null-aware spread (...?) instead of checking for a potential null value.
...localSet != null ? localSet : <String>{}, // LINT: Prefer null-aware spread (...?) with the then branch expression.
...localSet ?? {}, // LINT: Prefer null-aware spread (...?) instead of if-null (??).
];
}

✅ Good:

void fn() {
final Set<String>? localSet = <String>{};

final collection = [
...?localSet,
...?localSet,
...?localSet,
];
}