convertHeight function

double convertHeight(
  1. dynamic height,
  2. String? heightUnit,
  3. String? targetUnit
)

Implementation

double convertHeight(dynamic height, String? heightUnit, String? targetUnit) {
  final h = (height is num)
      ? height.toDouble()
      : double.tryParse(height.toString()) ?? 0.0;
  if (heightUnit == null ||
      heightUnit.isEmpty ||
      heightUnit == msgSelectAnOption ||
      targetUnit == null ||
      targetUnit.isEmpty ||
      targetUnit == msgSelectAnOption) {
    return 0.0;
  }
  if (heightUnit == targetUnit) {
    return h;
  }
  // cm <-> m
  if (heightUnit == 'cm' && targetUnit == 'm') return h / 100;
  if (heightUnit == 'm' && targetUnit == 'cm') return h * 100;
  // inches <-> m
  if (heightUnit == 'i' && targetUnit == 'm') return h * 0.0254;
  if (heightUnit == 'm' && targetUnit == 'i') return h / 0.0254;
  // inches <-> cm
  if (heightUnit == 'i' && targetUnit == 'cm') return h * 2.54;
  if (heightUnit == 'cm' && targetUnit == 'i') return h / 2.54;

  logErrorRaw(
    'Unsupported height conversion from "$heightUnit" to "$targetUnit"',
  );
  return h;
}