mapNotNull<R> method
Returns a new lazy Iterable containing the non-null results of applying
the given transform function to each element of this collection.
The transform function is invoked for each element in the collection, and
its result is included in the resulting iterable only if it is not null.
-
Parameters:
- transform: A function that takes an element of type
Tand returns a result of typeR.
- transform: A function that takes an element of type
-
Returns: A new lazy Iterable containing the non-null results of applying the
transformfunction to each element of this collection. -
Example:
Iterable<String> words = ['apple', 'banana', 'cherry']; Iterable<int> wordLengths = words.mapNotNull((word) { if (word.length > 5) { return word.length; } else { return null; } }); print(wordLengths); // Output: [6, 6]
Note:
This method creates a new lazy Iterable and does not modify the original
collection. It allows you to filter out null results from the transformation.
Implementation
Iterable<R> mapNotNull<R>(R transform(T element)) sync* {
for (var element in this) {
var result = transform(element);
if (result != null) {
yield result;
}
}
}