safeNormalized method

double safeNormalized(
  1. num fromMin,
  2. num fromMax, {
  3. num toMin = 0.0,
  4. num toMax = 1.0,
  5. double fallback = 0.0,
})

Like normalized but returns fallback instead of throwing when source range is zero. Useful in reactive/UI code where division by zero is a transient state rather than a programmer error.

Implementation

double safeNormalized(
  num fromMin,
  num fromMax, {
  num toMin = 0.0,
  num toMax = 1.0,
  double fallback = 0.0,
}) {
  final range = fromMax - fromMin;
  if (range == 0) return fallback;
  return ((toMax - toMin) * ((this - fromMin) / range) + toMin).toDouble();
}