convertWeight static method

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

Convert weight units

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

Implementation

static double convertWeight(double value, String fromUnit, String toUnit) {
  const Map<String, double> conversions = {
    'mg': 0.001,
    'g': 1.0,
    'kg': 1000.0,
    'ton': 1000000.0,
    'ounce': 28.3495,
    'pound': 453.592,
  };

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

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