checkInvariant method

void checkInvariant(
  1. String operation
)

Asserts that the invariant is satisfied for operation.

Call this after any state-modifying operation to verify the object remains in a valid state. Fails in debug mode with an assertion error if the invariant is violated.

Parameters:

  • operation: Description of the operation that was performed

Example:

void updateBalance(double amount) {
  balance += amount;
  checkInvariant('updateBalance');
}

Implementation

void checkInvariant(String operation) {
  assert(invariant(), '🔴 Invariant broken after: $operation');
}