diff --git a/lib/models/nutrition/ingredient.dart b/lib/models/nutrition/ingredient.dart index 1a0981f8..a8d4b565 100644 --- a/lib/models/nutrition/ingredient.dart +++ b/lib/models/nutrition/ingredient.dart @@ -26,7 +26,7 @@ part 'ingredient.g.dart'; class Ingredient { // fields returned by django api that we ignore here: // uuid, last_updated, last_imported, weight_units, language - // all license fields + // most license fields @JsonKey(required: true) final int id; @@ -42,6 +42,9 @@ class Ingredient { @JsonKey(required: true, name: 'source_url') final String? sourceUrl; + @JsonKey(required: true, name: 'license_object_url') + final String? licenseObjectURl; + /// Barcode of the product @JsonKey(required: true) final String? code; @@ -91,6 +94,7 @@ class Ingredient { required this.remoteId, required this.sourceName, required this.sourceUrl, + this.licenseObjectURl, required this.id, required this.code, required this.name, diff --git a/lib/models/nutrition/ingredient.g.dart b/lib/models/nutrition/ingredient.g.dart index f3f21074..6c820420 100644 --- a/lib/models/nutrition/ingredient.g.dart +++ b/lib/models/nutrition/ingredient.g.dart @@ -14,6 +14,7 @@ Ingredient _$IngredientFromJson(Map json) { 'remote_id', 'source_name', 'source_url', + 'license_object_url', 'code', 'name', 'created', @@ -31,6 +32,7 @@ Ingredient _$IngredientFromJson(Map json) { remoteId: json['remote_id'] as String?, sourceName: json['source_name'] as String?, sourceUrl: json['source_url'] as String?, + licenseObjectURl: json['license_object_url'] as String?, id: (json['id'] as num).toInt(), code: json['code'] as String?, name: json['name'] as String, @@ -55,6 +57,7 @@ Map _$IngredientToJson(Ingredient instance) => 'remote_id': instance.remoteId, 'source_name': instance.sourceName, 'source_url': instance.sourceUrl, + 'license_object_url': instance.licenseObjectURl, 'code': instance.code, 'name': instance.name, 'created': instance.created.toIso8601String(), diff --git a/lib/widgets/nutrition/helpers.dart b/lib/widgets/nutrition/helpers.dart index 20dfb7d7..a6659fbd 100644 --- a/lib/widgets/nutrition/helpers.dart +++ b/lib/widgets/nutrition/helpers.dart @@ -110,15 +110,11 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) { Ingredient? ingredient; NutritionalGoals? goals; String? source; - String? url; if (snapshot.hasData) { ingredient = snapshot.data; goals = ingredient!.nutritionalValues.toGoals(); source = ingredient.sourceName ?? 'unknown'; - url = ingredient.remoteId == null - ? null - : 'https://world.openfoodfacts.org/product/${ingredient.remoteId}'; } return AlertDialog( title: (snapshot.hasData) ? Text(ingredient!.name) : null, @@ -131,7 +127,7 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) { if (image != null) const SizedBox(height: 12), if (snapshot.hasError) Text( - 'Ingredient lookup error: ${snapshot.error}', + 'Ingredient lookup error: ${snapshot.error ?? 'unknown error'}', style: const TextStyle(color: Colors.red), ), if (!snapshot.hasData && !snapshot.hasError) const CircularProgressIndicator(), @@ -144,13 +140,14 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) { showGperKg: false, ), ), - if (snapshot.hasData && url == null) Text('Source: ${source!}'), - if (snapshot.hasData && url != null) + if (snapshot.hasData && ingredient!.licenseObjectURl == null) + Text('Source: ${source!}'), + if (snapshot.hasData && ingredient!.licenseObjectURl != null) Padding( padding: const EdgeInsets.only(top: 12), child: InkWell( child: Text('Source: ${source!}'), - onTap: () => launchURL(url!, context), + onTap: () => launchURL(ingredient!.licenseObjectURl!, context), ), ), ],