35 lines
995 B
Dart
35 lines
995 B
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
class FoodFactLookupClient {
|
|
FoodFactLookupClient();
|
|
|
|
static const String host = "world.openfoodfacts.org";
|
|
static const String url = "https://world.openfoodfacts.org/api/v3/product/";
|
|
|
|
String getProductUrl(String ean) {
|
|
return "$url$ean.json";
|
|
}
|
|
|
|
Future<FoodFactModel> retrieveFoodInfo(String ean) async {
|
|
HttpClient client = HttpClient();
|
|
var request = await client.getUrl(Uri.parse(getProductUrl(ean)));
|
|
var response = await request.close();
|
|
var asString = await response.transform(utf8.decoder).join();
|
|
return FoodFactModel.fromJson(jsonDecode(asString));
|
|
}
|
|
}
|
|
|
|
class FoodFactModel {
|
|
final String name;
|
|
final int kcalPer100g;
|
|
|
|
FoodFactModel({required this.name, required this.kcalPer100g});
|
|
|
|
factory FoodFactModel.fromJson(Map<String, dynamic> json) {
|
|
return FoodFactModel(
|
|
name: json['product']['product_name'],
|
|
kcalPer100g: json['product']['nutriments']['energy-kcal_100g']);
|
|
}
|
|
}
|