filter method

T? filter(
  1. bool f(
    1. T
    )
)

returns if value is not empty and matches given function f.

example

int? a = 2;
int? b = 3;
int? c = null;
a.filter((element) => element %2 == 0) // => 2
b.filter((element) => element %2 == 0) // => null
c.filter(...) // => null

Implementation

T? filter(bool Function(T) f) {
  if (this == null) {
    return null;
  } else {
    if (f(this!)) {
      return this!;
    } else {
      return null;
    }
  }
}