convertLength static method

double convertLength(
  1. double value,
  2. String fromUnit,
  3. String toUnit
)

Convert length units

value - Value to convert fromUnit - Source unit toUnit - Target unit Returns converted value

Implementation

static double convertLength(double value, String fromUnit, String toUnit) {
  const Map<String, double> conversions = {
    'mm': 0.001,
    'cm': 0.01,
    'm': 1.0,
    'km': 1000.0,
    'inch': 0.0254,
    'foot': 0.3048,
    'yard': 0.9144,
    'mile': 1609.34,
  };

  if (!conversions.containsKey(fromUnit) || !conversions.containsKey(toUnit)) {
    throw ArgumentError('Invalid length units');
  }

  return value * conversions[fromUnit]! / conversions[toUnit]!;
}