openfoodfacts 0.1.11

  • Readme
  • Changelog
  • Example
  • Installing
  • 81

Open Food Facts - Dart #

Dart package for the Open Food Facts API.

Content #

The following API services are implemented:

getProduct(barcode, language) #

Get the product information for an existing barcode. Prepare ingredients, images and product name depending on the specified language.

getProductRaw(barcode) #

Get the product information for an existing barcode without additional preparation.

saveProduct(user, product) #

Add a new product or edit an existing one, using the specified Open Food Facts user credentials.

addProductImage(user, image) #

Add an product image of the front, the ingredients or the nutrition, using the specified Open Food Facts user credentials.

searchProducts(parameterList, language) #

Search for all products matching to the given parameters. Optional: use a language specific host.

Development #

Getting Started #

For help getting started with Flutter, view our online flutter docs. For help on editing package code, view the package docs.

auto generate the json classes #

By running (in command prompt) flutter packages pub run build_runner build in our project root, you can generate json serialization code for our models whenever needed.

running the unit tests #

Adjust the file test_constants.dart to provide your own login credentials (OpenFoodFacts).

publish the package to pub.dartlang.org #

run flutter packages pub publish in the project directory

Authors #

[0.1.11] - 9.10.2019

  • additives management implemented
  • nutrient levels implemented
  • added Nutriscore to product
  • bugfix: ingredientText language dependent

[0.1.10] - 5.10.2019

  • nutriments management implemented

[0.1.9] - 21.09.2019

  • get product image by field, size and language

[0.1.8] - 13.09.2019

  • http: 0.12.0

[0.1.7] - 13.09.2019

  • Bugfix: search result page - json parse error fixed (string vs. int)
  • json_annotation: 3.0.0
  • build_runner: 1.6.9
  • json_serializable: 3.2.2

[0.1.6] - 06.06.2019

  • Bugfix: ingredients percent json parse error fixed (string vs. int)

[0.1.5] - 28.05.2019

  • getProduct() fixed null-pointer for non existing product.
  • searchProducts() english URL now working. (-> world)

[0.1.4] - 19.05.2019

  • getProduct() chooses a language dependent host (world, de, ..)
  • Bugfix: getProduct() won't fail if no images are provided

[0.1.3] - 5.05.2019

  • searchProducts() with language support

[0.1.2] - 5.05.2019

  • Ingredients.dart more type safe (THX to dynamic)

[0.1.1] - 5.05.2019

  • class SearchResult now visible

[0.1.0] - 4.05.2019.

  • service method searchProducts() implemented.

[0.0.10] - 28.03.2019

the test package dependency has been moved to flutter_test.

[0.0.9] - 16.03.2019.

dependency version upgrade to:

  • json_annotation: 2.0.0
  • build_runner: 1.2.6
  • json_serializable: 2.0.2

[0.0.8] - 16.11.2018.

  • getProduct() provides ingredients and images for a given language.
  • getProductRaw() provides all data fields without preparation.

[0.0.6] - 27.10.2018.

  • set dependency versions as range

[0.0.5] - 25.10.2018.

  • upgrade of http 0.12.0
  • upgrade of build_runner 1.0.0

[0.0.4] - 16.10.2018.

  • class Image changed to ProductImage for a easier namespace resolving.

[0.0.3] - 12.10.2018.

  • model of selected product images changed.

[0.0.2] - 3.10.2018.

  • Service saveProduct() can handle to add and to edit a product.
  • Added traces to a product

[0.0.1] - 18.09.2018.

initial version with these API services:

  • add a new product
  • add a new product image
  • get an existing product

example/lib/main.dart

import 'package:openfoodfacts/openfoodfacts.dart';

/// request a product from the OpenFoodFacts database
Future<Product> getProduct() async {
  var barcode = "0048151623426";
  ProductResult result =
      await OpenFoodAPIClient.getProduct("barcode", User.LANGUAGE_DE);

  if (result.status == 1) {
    return result.product;
  } else {
    throw new Exception("product not found, please insert data for " + barcode);
  }
}

/// add a new product to the OpenFoodFacts database
void addNewProduct() async {
  // define the product to be added.
  // more attributes available ...
  Product myProduct = new Product(
    barcode: "0048151623426",
    productName: "Maryland Choc Chip",
  );

  // a registered user login for https://world.openfoodfacts.org/ is required
  User myUser = new User(userId: "max@off.com", password: "password");

  // query the OpenFoodFacts API
  Status result = await OpenFoodAPIClient.saveProduct(myUser, myProduct);

  if (result.status != 1) {
    throw new Exception("product could not be added: " + result.error);
  }
}

/// add a new image for an existing product of the OpenFoodFacts database
void addProductImage() async {
  // define the product image
  // set the uri to the local image file
  // choose the "imageField" as location / description of the image content.
  SendImage image = new SendImage(
    lang: "en",
    barcode: "0048151623426",
    imageField: ProductImage.FIELD_INGREDIENTS,
    imageUrl: Uri.parse("assets/ingredients_en.jpg"),
  );

  // a registered user login for https://world.openfoodfacts.org/ is required
  User myUser = new User(userId: "max@off.com", password: "password");

  // query the OpenFoodFacts API
  Status result = await OpenFoodAPIClient.addProductImage(myUser, image);

  if (result.status != "status ok") {
    throw new Exception("image could not be uploaded: " +
        result.error +
        " " +
        result.imageId.toString());
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  openfoodfacts: ^0.1.11

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:openfoodfacts/openfoodfacts.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
64
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on Oct 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/openfoodfacts.dart. (-0.50 points)

Analysis of lib/openfoodfacts.dart reported 1 hint:

line 144 col 25: The declaration '_login' isn't referenced.

Fix lib/utils/HttpHelper.dart. (-0.50 points)

Analysis of lib/utils/HttpHelper.dart reported 1 hint:

line 70 col 47: This function has a return type of 'Future

Fix lib/utils/ImageHelper.dart. (-0.50 points)

Analysis of lib/utils/ImageHelper.dart reported 1 hint:

line 2 col 8: Unused import: '../model/ProductImage.dart'.

Format lib/utils/ProductHelper.dart.

Run flutter format to format lib/utils/ProductHelper.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.54.0 <3.0.0
async_loader ^0.1.2 0.1.2
flutter 0.0.0
http ^0.12.0 0.12.0+2
image ^2.1.4 2.1.4
json_annotation ^3.0.0 3.0.0
path ^1.6.4 1.6.4
Transitive dependencies
archive 2.0.10
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3
http_parser 3.1.3
meta 1.1.7
pedantic 1.8.0+1
petitparser 2.4.0
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
xml 3.5.0
Dev dependencies
build_runner ^1.6.9
flutter_test
json_serializable ^3.2.2