convertLength function Null safety

double convertLength(
  1. Length from,
  2. Length to,
  3. double value
)

Convert Length. 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 length = convertLength(
   Length.centimeter, // from
   Length.meter, // to
   100, // value
 );
 print(length); // 1
}

Read the documentation for more information

Implementation

double convertLength(Length from, Length to, double value) {
  verify(from, to, value);
  if (from == to) return value;
  if (from == Length.meter) {
    switch (to) {
      case Length.centimeter:
        return value * 100;
      case Length.kilometer:
        return value / 1000;
      case Length.millimeter:
        return value * 1000;
      case Length.micrometer:
        return value * 1e+6;
      case Length.nanometer:
        return value * 1e+9; // approach
      case Length.mile:
        return value / 1609; // approach
      case Length.yard:
        return value * 1.094; // approach
      case Length.feet:
        return value * 3.281; // approach
      case Length.inch:
        return value * 39.37;
      case Length.nautical_mile:
        return value / 1852;
      case Length.meter:
        return value;
      default:
        return value;
    }
  } else {
    switch (from) {
      case Length.centimeter:
        return convertLength(Length.meter, to, value * 100);
      case Length.kilometer:
        return convertLength(Length.meter, to, value * 1000);
      case Length.millimeter:
        return convertLength(Length.meter, to, value / 1000);
      case Length.micrometer:
        return convertLength(Length.meter, to, value / 1e+6);
      case Length.nanometer:
        return convertLength(Length.meter, to, value / 1e+9); // approach
      case Length.mile:
        return convertLength(Length.meter, to, value * 1609); // approach
      case Length.yard:
        return convertLength(Length.meter, to, value / 1.094); // approach
      case Length.feet:
        return convertLength(Length.meter, to, value / 3.281); // approach
      case Length.inch:
        return convertLength(Length.meter, to, value / 39.37);
      case Length.nautical_mile:
        return convertLength(Length.meter, to, value * 1852);
      default:
        return value;
    }
  }
}