convertTemperature function Null safety

double convertTemperature(
  1. Temperature from,
  2. Temperature to,
  3. double value
)

Convert Temperature. It may lose some decimals in the proccess.

from, to and value can NOT be null, otherwise an AssertionError is thrown

Basic usage:

main() {
  final temperature = convertTemperature(
    Temperature.celcius, // from
    Temperature.fahrenheit, // to
    0, // value
  );
  print(temperature); // 32
}

Read the documentation for more information

Implementation

double convertTemperature(Temperature from, Temperature to, double value) {
  verify(from, to, value);
  if (from == to) return value;
  if (from == Temperature.celcius) {
    if (to == Temperature.fahrenheit) return (value * 9 / 5) + 32;
    if (to == Temperature.kelvin) return value + 273.15;
  } else if (from == Temperature.fahrenheit) {
    if (to == Temperature.celcius) return (value - 32) * 5 / 9;
    if (to == Temperature.kelvin) return (value - 32) * 5 / 9 + 273.15;
  } else if (from == Temperature.kelvin) {
    if (to == Temperature.celcius) return value - 273.15;
    if (to == Temperature.fahrenheit) return (value - 273.15) * 9 / 5 + 32;
  }
  return value;
}