convertPressure function Null safety

double convertPressure(
  1. Pressure from,
  2. Pressure to,
  3. double value
)

Convert Pressure. 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 pressure = convertPressure(
    Pressure.bar, // from
    Pressure.pascal, // to
    1, // value
  );
  print(pressure); // 100000
}

Read the documentation for more information

Implementation

double convertPressure(Pressure from, Pressure to, double value) {
  verify(from, to, value);
  if (from == to) return value;
  if (from == Pressure.pascal) {
    switch (to) {
      case Pressure.bar:
        return value / 100000;
      case Pressure.poundForcePerSquareInch:
        return value / 6895; // approximate
      case Pressure.standartAtmosphere:
        return value / 101325;
      case Pressure.torr:
        return value / 133;
      default:
        return value;
    }
  } else {
    final pascal = Pressure.pascal;
    switch (from) {
      case Pressure.bar:
        return convertPressure(pascal, to, value * 100000);
      case Pressure.poundForcePerSquareInch:
        return convertPressure(pascal, to, value * 6895);
      case Pressure.standartAtmosphere:
        return convertPressure(pascal, to, value * 101325);
      case Pressure.torr:
        return convertPressure(pascal, to, value * 133);
      default:
        return value;
    }
  }
}