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
This commit is contained in:
Roland Geider
2026-04-14 00:10:27 +02:00
parent 86b2a93e87
commit d343e5cdf9
7 changed files with 55 additions and 8 deletions

View File

@@ -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,
});

View File

@@ -45,8 +45,9 @@ Ingredient _$IngredientFromJson(Map<String, dynamic> 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<String, dynamic>),
@@ -77,6 +78,15 @@ Map<String, dynamic> _$IngredientToJson(Ingredient instance) => <String, dynamic
'sodium': numToString(instance.sodium),
'is_vegan': instance.isVegan,
'is_vegetarian': instance.isVegetarian,
'nutriscore': _$NutriScoreEnumMap[instance.nutriscore],
'image': instance.image,
'thumbnails': instance.thumbnails,
};
const _$NutriScoreEnumMap = {
NutriScore.a: 'a',
NutriScore.b: 'b',
NutriScore.c: 'c',
NutriScore.d: 'd',
NutriScore.e: 'e',
};

View File

@@ -17,6 +17,9 @@
"fat_saturated": "0.000",
"fiber": null,
"sodium": "0.000",
"is_vegan": null,
"is_vegetarian": true,
"nutriscore": "d",
"weight_units": [],
"language": {
"id": 2,

View File

@@ -17,6 +17,9 @@
"fat_saturated": "0.714",
"fiber": "6.960",
"sodium": "0.000",
"is_vegan": null,
"is_vegetarian": null,
"nutriscore": null,
"weight_units": [],
"language": {
"id": 1,

View File

@@ -17,6 +17,9 @@
"fat_saturated": "0.100",
"fiber": "9.100",
"sodium": "0.260",
"is_vegan": true,
"is_vegetarian": true,
"nutriscore": "c",
"weight_units": [],
"language": {
"id": 1,

View File

@@ -22,6 +22,9 @@
"fat_saturated": "0.000",
"fiber": null,
"sodium": "0.020",
"is_vegan": true,
"is_vegetarian": true,
"nutriscore": "a",
"weight_units": [],
"language": {
"id": 12,

View File

@@ -41,6 +41,9 @@ final ingredient1 = Ingredient(
fatSaturated: 7,
fiber: 12,
sodium: 0.5,
isVegan: true,
isVegetarian: true,
nutriscore: NutriScore.a,
);
final ingredient2 = Ingredient(
remoteId: '2',
@@ -92,6 +95,9 @@ final muesli = Ingredient(
fatSaturated: 7,
fiber: 12,
sodium: 0.5,
isVegan: true,
isVegetarian: true,
nutriscore: NutriScore.b,
);
final milk = Ingredient(
remoteId: '1',
@@ -109,6 +115,8 @@ final milk = Ingredient(
fatSaturated: 7,
fiber: 12,
sodium: 0.5,
isVegetarian: true,
nutriscore: NutriScore.c,
);
final apple = Ingredient(
remoteId: '1',