From d343e5cdf938170ba98b164646bfc7d2d73ac008 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 14 Apr 2026 00:10:27 +0200 Subject: [PATCH] Add missing nutriscore info to the ingredient model Also, make the vegan and vegetarian flags nullable, since not all products have this information at all --- lib/models/nutrition/ingredient.dart | 29 +++++++++++++++---- lib/models/nutrition/ingredient.g.dart | 14 +++++++-- .../nutrition/ingredientinfo_10065.json | 3 ++ .../nutrition/ingredientinfo_58300.json | 3 ++ .../nutrition/ingredientinfo_59887.json | 3 ++ .../nutrition/ingredientinfo_right_code.json | 3 ++ test_data/nutritional_plans.dart | 8 +++++ 7 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/models/nutrition/ingredient.dart b/lib/models/nutrition/ingredient.dart index a72405bb2..be64ae5db 100644 --- a/lib/models/nutrition/ingredient.dart +++ b/lib/models/nutrition/ingredient.dart @@ -23,6 +23,19 @@ import 'package:wger/models/nutrition/nutritional_values.dart'; part 'ingredient.g.dart'; +enum NutriScore { + @JsonValue('a') + a, + @JsonValue('b') + b, + @JsonValue('c') + c, + @JsonValue('d') + d, + @JsonValue('e') + e, +} + @JsonSerializable() class Ingredient { // fields returned by django api that we ignore here: @@ -89,11 +102,14 @@ class Ingredient { @JsonKey(required: true, fromJson: stringToNum, toJson: numToString) final num sodium; - @JsonKey(name: 'is_vegan', defaultValue: false) - final bool isVegan; + @JsonKey(name: 'is_vegan') + final bool? isVegan; - @JsonKey(name: 'is_vegetarian', defaultValue: false) - final bool isVegetarian; + @JsonKey(name: 'is_vegetarian') + final bool? isVegetarian; + + @JsonKey(name: 'nutriscore') + final NutriScore? nutriscore; IngredientImage? image; @@ -116,8 +132,9 @@ class Ingredient { required this.fatSaturated, required this.fiber, required this.sodium, - this.isVegan = false, - this.isVegetarian = false, + this.isVegan, + this.isVegetarian, + this.nutriscore, this.image, this.thumbnails, }); diff --git a/lib/models/nutrition/ingredient.g.dart b/lib/models/nutrition/ingredient.g.dart index ed1b36ab3..39c74cad6 100644 --- a/lib/models/nutrition/ingredient.g.dart +++ b/lib/models/nutrition/ingredient.g.dart @@ -45,8 +45,9 @@ Ingredient _$IngredientFromJson(Map json) { fatSaturated: stringToNum(json['fat_saturated'] as String?), fiber: stringToNum(json['fiber'] as String?), sodium: stringToNum(json['sodium'] as String?), - isVegan: json['is_vegan'] as bool? ?? false, - isVegetarian: json['is_vegetarian'] as bool? ?? false, + isVegan: json['is_vegan'] as bool?, + isVegetarian: json['is_vegetarian'] as bool?, + nutriscore: $enumDecodeNullable(_$NutriScoreEnumMap, json['nutriscore']), image: json['image'] == null ? null : IngredientImage.fromJson(json['image'] as Map), @@ -77,6 +78,15 @@ Map _$IngredientToJson(Ingredient instance) =>