post method

T post(
  1. bool condition(
    1. T
    ),
  2. String message
)

Checks a postcondition on this value and returns it.

Postconditions verify that a result satisfies expected conditions. The condition function receives this value and should return true if the postcondition is satisfied.

The assertion runs in a finally block, ensuring it executes before the value is returned to the caller.

Parameters:

  • condition: Predicate function that verifies the postcondition
  • message: Error message if the postcondition fails

Returns: This value (allowing for method chaining)

Example:

final result = compute()
  .post((r) => r >= 0, 'Result must be non-negative');

Implementation

T post(bool Function(T) condition, String message) {
  assert(condition(this), '🔴 Postcondition: $message');
  return this;
}