EnergyService constructor

EnergyService({
  1. int maxEnergy = 5,
  2. Duration refillInterval = const Duration(minutes: 30),
})

Implementation

EnergyService({
  this.maxEnergy = 5,
  this.refillInterval = const Duration(minutes: 30),
}) {
  // BUG-19: a misconfigured maxEnergy/refillInterval (a caller passing a
  // hardcoded or remote-config-derived value that turns out <= 0) must
  // fail loudly at construction time rather than silently producing
  // nonsensical energy math (an always-empty/always-full bar, or a
  // division that never terminates a refill tick). A plain runtime
  // check (not an `assert`) so it still fires in release builds, where
  // asserts are stripped.
  if (maxEnergy <= 0) {
    throw ArgumentError.value(maxEnergy, 'maxEnergy', 'must be > 0');
  }
  if (refillInterval <= Duration.zero) {
    throw ArgumentError.value(
      refillInterval,
      'refillInterval',
      'must be > 0',
    );
  }
}