pre method

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

Checks a precondition on this value and returns it.

Note: Despite the name "precondition", this checks the condition AFTER returning from the method (in the finally block). This is due to the try-finally implementation pattern used here.

For traditional precondition checking (fail fast before using a value), consider checking conditions directly:

assert(condition, 'message');
final value = someOperation();

The finally block ensures the assertion runs and the method still returns the value (or throws if assertion fails).

Parameters:

  • condition: Boolean that must be true for the precondition
  • message: Error message if the precondition fails

Returns: This value (allowing for method chaining)

Example:

final adult = person
  .pre(person.age >= 18, 'Person must be adult');

Implementation

T pre(bool condition, String message) {
  assert(condition, '🔴 Precondition: $message');
  return this;
}