CompactMap<T> extension

Extension on Iterable of nullable values, allowing removal of nulls and optional transformation.

If a transform function is provided, it is applied to each element before filtering out nulls.

Example:

final result = [1, 2, null, 3].compactMap();
print(result); // (1, 2, 3)

final transformed = [1, 2, null, 3].compactMap((e) => e == null ? null : e * 2);
print(transformed); // (2, 4, 6)
on

Methods

compactMap<E>([E? transform(T?)?]) Iterable<T>

Available on Iterable<T?>, provided by the CompactMap extension

Maps and filters out null values from the iterable.