takeIf method

T? takeIf(
  1. bool test(
    1. T it
    )
)

Returns this value if it satisfies the given predicate test or null if it doesn't.

Example:

int? number = 5.takeIf((it) => it > 3);
print(number); // Output: 5

number = 5.takeIf((it) => it > 6);
print(number); // Output: null

Implementation

T? takeIf(bool Function(T it) test) => test(this) ? this : null;