calculateEnergy static method

double? calculateEnergy(
  1. Nutriments nutriments, {
  2. PerSize perSize = PerSize.oneHundredGrams,
})

Calculates the energy for 100g in kJ. ! should be used cautiously (might not be displayed to the end user) ! source : https://en.wikipedia.org/wiki/Food_energy

Implementation

static double? calculateEnergy(
  Nutriments nutriments, {
  PerSize perSize = PerSize.oneHundredGrams,
}) {
  double? fat = nutriments.getValue(Nutrient.fat, perSize);
  double? carbs = nutriments.getValue(Nutrient.carbohydrates, perSize);
  double? proteins = nutriments.getValue(Nutrient.proteins, perSize);
  double? fiber = nutriments.getValue(Nutrient.fiber, perSize);

  if (fat == null || carbs == null || proteins == null || fiber == null) {
    return null;
  }

  return (fat * 37 + carbs * 17 + proteins * 17 + fiber * 8);
}