mapNotNull<R> method

List<R> mapNotNull<R>(
  1. R? transform(
    1. T
    )
)

Maps each element using transform and removes null results.

Example:

final input = ["1", "x", "2"];
final output = input.mapNotNull((s) => int.tryParse(s));
-> output = [1, 2]

Implementation

List<R> mapNotNull<R>(R? Function(T) transform) {
  final output = <R>[];
  for (final item in this) {
    final value = transform(item);
    if (value != null) output.add(value);
  }
  return output;
}