applyIf method

T applyIf(
  1. bool condition,
  2. T block(
    1. T value
    )
)

Conditionally transforms this value and returns the result.

If condition is true, applies the block transformation to this value and returns the result. Otherwise, returns this value unchanged. The block must return another value of the same type T.

Use this for simple boolean conditions where you have a transform to apply if the condition is true.

Example:

final user = User(name: 'Alice', role: 'user')
  .applyIf(isAdmin, (u) => u.copyWith(role: 'admin', level: 10));
// If isAdmin is true, user becomes an admin with level 10
// If isAdmin is false, user remains unchanged

Implementation

T applyIf(bool condition, T Function(T value) block) {
  if (condition) {
    return block(this);
  }

  return this;
}