TakeIf<T> extension

takeIf returns the current value if the given block is satisifed, will return null if not.

takeIf is a filtering function for a single object.

final number = Random().nextInt(10);
final evenOrNull = number.takeIf((it) => it % 2 == 0);
print('even: $evenOrNull');

When chaining other functions after takeIf, don't forget to use the ?. safe call or perform a null check.

on
  • T?

Methods

takeIf(bool block(T it)) → T?

Available on T?, provided by the TakeIf extension

Return the current value if the given block is satisifed, will return null if not.