openfoodfacts 0.1.6

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.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.6

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]
60
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/model/parameter/OutputFormat.dart. (-1 points)

Analysis of lib/model/parameter/OutputFormat.dart reported 2 hints:

line 17 col 6: Dead code.

line 17 col 6: Avoid empty statements.

Fix lib/model/ImageList.dart. (-0.50 points)

Analysis of lib/model/ImageList.dart reported 1 hint:

line 1 col 8: Unused import: 'package:json_annotation/json_annotation.dart'.

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

Analysis of lib/openfoodfacts.dart reported 1 hint:

line 153 col 25: The method '_login' isn't used.

Fix additional 17 files with analysis or formatting issues. (-0.50 points)

Additional issues in the following files:

  • lib/utils/HttpHelper.dart (1 hint)
  • lib/interface/JsonObject.dart (Run flutter format to format lib/interface/JsonObject.dart.)
  • lib/interface/Parameter.dart (Run flutter format to format lib/interface/Parameter.dart.)
  • lib/model/Ingredient.dart (Run flutter format to format lib/model/Ingredient.dart.)
  • lib/model/Product.dart (Run flutter format to format lib/model/Product.dart.)
  • lib/model/ProductImage.dart (Run flutter format to format lib/model/ProductImage.dart.)
  • lib/model/ProductResult.dart (Run flutter format to format lib/model/ProductResult.dart.)
  • lib/model/SearchResult.dart (Run flutter format to format lib/model/SearchResult.dart.)
  • lib/model/SendImage.dart (Run flutter format to format lib/model/SendImage.dart.)
  • lib/model/Status.dart (Run flutter format to format lib/model/Status.dart.)
  • lib/model/TagI18N.dart (Run flutter format to format lib/model/TagI18N.dart.)
  • lib/model/User.dart (Run flutter format to format lib/model/User.dart.)
  • lib/model/parameter/Page.dart (Run flutter format to format lib/model/parameter/Page.dart.)
  • lib/model/parameter/PageSize.dart (Run flutter format to format lib/model/parameter/PageSize.dart.)
  • lib/model/parameter/SearchSimple.dart (Run flutter format to format lib/model/parameter/SearchSimple.dart.)
  • lib/model/parameter/SortBy.dart (Run flutter format to format lib/model/parameter/SortBy.dart.)
  • lib/utils/ProductHelper.dart (Run flutter format to format lib/utils/ProductHelper.dart.)

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (http).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

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.11.0 0.11.3+17 0.12.0+2
json_annotation ^2.0.0 2.4.0
path ^1.6.2 1.6.2
Transitive dependencies
async 2.2.0
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
meta 1.1.6 1.1.7
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.2.6
flutter_test
json_serializable ^2.0.2

Admin