checkEnergyCoherence static method

bool checkEnergyCoherence(
  1. Nutriments nutriments,
  2. double marginPercentage,
  3. {PerSize perSize = PerSize.oneHundredGrams}
)

Checks if the stated energy value is within a margin of error a use case for this is before saving a product, check if the values aren't incoherent.

Implementation

static bool checkEnergyCoherence(
  Nutriments nutriments,
  double marginPercentage, {
  PerSize perSize = PerSize.oneHundredGrams,
}) {
  final double statedEnergy = nutriments.getComputedKJ(perSize)!;

  double lowLimit =
      statedEnergy - (statedEnergy * (marginPercentage / 100.0));
  double highLimit =
      statedEnergy + (statedEnergy * (marginPercentage / 100.0));

  double calculatedEnergy = calculateEnergy(nutriments)!;

  return (calculatedEnergy >= lowLimit && calculatedEnergy <= highLimit);
}