convertWeight function

double convertWeight(
  1. dynamic weight,
  2. String? weightUnit,
  3. String? targetUnit
)

Implementation

double convertWeight(dynamic weight, String? weightUnit, String? targetUnit) {
  final w = (weight is num)
      ? weight.toDouble()
      : double.tryParse(weight.toString()) ?? 0.0;
  if (weightUnit == null ||
      weightUnit.isEmpty ||
      weightUnit == msgSelectAnOption ||
      targetUnit == null ||
      targetUnit.isEmpty ||
      targetUnit == msgSelectAnOption) {
    return 0.0;
  }
  if (weightUnit == targetUnit) {
    return w;
  }
  if (weightUnit == 'kg' && targetUnit == 'lb') return w * 2.20462;
  if (weightUnit == 'lb' && targetUnit == 'kg') return w / 2.20462;

  logErrorRaw(
    'Unsupported weight conversion from "$weightUnit" to "$targetUnit"',
  );
  return w;
}