convertTemperature static method

double convertTemperature(
  1. double temperature,
  2. String fromUnit,
  3. String toUnit
)

Convert temperature between Celsius and Fahrenheit

temperature - Temperature value fromUnit - Source unit ('celsius' or 'fahrenheit') toUnit - Target unit ('celsius' or 'fahrenheit') Returns converted temperature

Implementation

static double convertTemperature(double temperature, String fromUnit, String toUnit) {
  if (fromUnit.toLowerCase() == toUnit.toLowerCase()) return temperature;

  if (fromUnit.toLowerCase() == 'celsius' && toUnit.toLowerCase() == 'fahrenheit') {
    return (temperature * 9 / 5) + 32;
  } else if (fromUnit.toLowerCase() == 'fahrenheit' && toUnit.toLowerCase() == 'celsius') {
    return (temperature - 32) * 5 / 9;
  }

  throw ArgumentError('Invalid temperature units');
}