takeIf method

T? takeIf(
  1. bool condition
)

Returns this value if condition is true, otherwise null.

A simple boolean check that conditionally returns the value. Useful for filtering in chains.

Example:

final adult = person.takeIf(person.age >= 18);
// adult is person if age >= 18, null otherwise

Implementation

T? takeIf(bool condition) {
  if (condition) return this;

  return null;
}