mapNotNull<R> method

Iterable<R> mapNotNull<R>(
  1. R transform(
    1. T element
    )
)

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 T and returns a result of type R.
  • Returns: A new lazy Iterable containing the non-null results of applying the transform function 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;
    }
  }
}