Skip to main content

prefer-null-aware-spread

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

✅ Good:

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

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