fetchRobotoffExtraction method
Implementation
Future<bool> fetchRobotoffExtraction(Product product) async {
if (product.barcode == null) {
return false;
}
_loadingRobotoffExtraction = true;
_robotoffNutrientExtraction = null;
notifyListeners();
final RobotoffNutrientExtractionResult extractionResult =
await RobotoffAPIClient.getNutrientExtraction(
product.barcode!,
uriHelper: ProductQuery.uriRobotoffHelper,
);
final bool extractionSuccessful = extractionResult.status == 'found';
if (extractionSuccessful) {
// When using Robotoff extraction we enforce the perSize to 100g
perSize = PerSize.oneHundredGrams;
Set<String> extractedNutrients =
extractionResult.latestInsight?.data?.nutrients?.keys.toSet() ??
<String>{};
extractedNutrients = extractedNutrients
.where((String nutrient) => !nutrient.contains('_serving'))
.map((String key) => key.replaceAll('_100g', ''))
.toSet();
for (final String nutrientOffTag in extractedNutrients) {
// If the nutrient is not in the list of nutrients, we add it
final OrderedNutrient? missingNutrient = getLeftoverNutrients()
.firstWhereOrNull((OrderedNutrient orderedNutrient) {
return orderedNutrient.nutrient?.offTag == nutrientOffTag;
});
if (missingNutrient != null) {
add(missingNutrient);
}
}
for (final OrderedNutrient orderedNutrient in _nutrients) {
final Nutrient nutrient = getNutrient(orderedNutrient)!;
final RobotoffNutrientEntity? robotoffNutrientEntity = extractionResult
.getNutrientEntity(nutrient, perSize);
if (robotoffNutrientEntity != null) {
AnalyticsHelper.trackRobotoffExtraction(
AnalyticsRobotoffEvents.robotoffNutritionExtracted,
nutrient,
product,
);
}
}
_robotoffNutrientExtraction = extractionResult;
}
_loadingRobotoffExtraction = false;
notifyListeners();
return extractionSuccessful;
}