withNullsRemoved method
Returns a new List with all null
s removed.
This may return a list with a non-nullable type.
See also: removeNulls
, which mutates the list (and does not change its type).
Example:
List<String?> myList = ["a", "b", null];
// Is ["a", "b"]
List<String> myNewList = myList.withNullsRemoved();
Implementation
List<T> withNullsRemoved() {
Iterable<T> _whereNotNull() sync* {
for (var element in this) {
if (element != null) yield element;
}
}
return _whereNotNull().toList();
}