Open Food Facts - Dart

Pub Version Run sdk tests likes popularity pub points

Dart package for the Open Food Facts API. Free and Easy access to more than 2.9 million food products information from all around the world. Open Food Facts is powered by contributors from around the world and is constantly growing thanks to them.

General principles, how does it work ?

We use the ability of the Open Food Facts API to return products results in JSON, we then generate easily understandable objects structures to make it simple for you to use.

This plugin also allows you to edit a product or upload a new one to Open Food Facts. Using the same simple product structure you can create a product object or edit an existing one and send it to the API using a single function.

Migrating from 2.x.x to 3.x.x (breaking changes)

  • Starting with version 3.0.0, we now enforce all clients to provide a valid user agent. For this, please ensure to set the SDK before using any other functionality:
OpenFoodAPIConfiguration.userAgent = UserAgent(
  name: '<Name of your app>',
);
  • QueryType has been deleted. Now, for API calls you have to provide a UriProductHelper parameter. By default it will point you to openfoodfacts/prod.

  • For RobotoffAPIClient.getRandomInsights and RobotoffAPIClient.getQuestions, a list of countries instead of a single country as parameter.

  • Use OpenFoodFactsCountry.fromOffTag instead of CountryHelper.fromJson.

  • OpenFoodAPIClient.getOrderedNutrients now uses a OpenFoodFactsCountry parameter instead of a 2-letter country code.

  • Methods getProductImageRootUrl and getBarcodeSubPath are moved to UriProductHelper from ImageHelper

  • Method buildUrl renamed as getLocalizedProductImageUrl in ImageHelper

  • Removal of deprecated code.

Migrating from 1.x.x to 2.x.x (breaking changes)

  • Now the only entry point is import 'package:openfoodfacts/openfoodfacts.dart';
    • replace all your instances of import 'package:openfoodfacts/...'; with a single import 'package:openfoodfacts/openfoodfacts.dart';
  • If you used State from product_state.dart, you have to rename it to ProductState
  • If you used Level from nutrient_levels.dart, you have to rename it to NutrientLevel
  • Removed deprecated classes:
    • Page
    • ProductListQueryConfiguration
    • ToBeCompletedConfiguration
  • Removed deprecated fields and methods in Nutriments
    • all the single nutrient value fields were removed - use getValue and setValue instead
    • instead of getUnit use nutrient.typicalUnit

Usage

Installation

Follow the installing instructions on pub.dev.

How to authenticate

For most queries no authentication is required! :) Though we recommend to set a User-Agent to not to be blocked by mistake.

Setup (Optional)

At the beginning of the app you can define some global settings so that they don't need to be specified in each query. You can override these static values at any time in the app lifecycle.

import 'package:openfoodfacts/openfoodfacts.dart';

OpenFoodAPIConfiguration.userAgent = UserAgent(name: 'Your app name', url: 'Your url, if applicable');

OpenFoodAPIConfiguration.globalLanguages = <OpenFoodFactsLanguage>[
  OpenFoodFactsLanguage.ENGLISH
];

OpenFoodAPIConfiguration.globalCountry = OpenFoodFactsCountry.FRANCE;

All possible configurations can be found here.

Features

Code examples for the following tasks:

Reading data

Writing data

Robotoff support

Robotoff it the Open Food Facts AI which analyze every new pictures to extract new data.

Folksonomy Engine

Folksonomy is adding several kinds of new individual data properties to Open Food Facts or Open Products Facts.

Contributing data

Handle Open Food Facts accounts

Some queries which modify or enter data need a user account to validate this request. There are multiple ways to handle user accounts:

  1. Let your users login / create Open Food Facts user accounts (Recommended)
  2. Create a global user for your app through which all requests run

Currently there is no OAuth workflow, therefor a user is just a User object in this package. So you need to get the username as well as the password from the users and store it somewhere save. For Flutter apps we recommend using the flutter_secure_storage package

For the user to be taken into account you have mount a global user at one point in your app lifecycle:

  OpenFoodAPIConfiguration.globalUser = User(
    userId: 'myUsername',
    password: 'myPassword',
  );

Regardless if you are using a global app or accounts per user. After mounting this, the user will be added to queries where the user can be attributed.

Some methods in OpenFoodAPIClient require to pass a User, there you can access the before mounted user with:

 OpenFoodAPIClient.thisNeedsAnUser(
  user: OpenFoodAPIConfiguration.globalUser,
  ...
 );

Further examples:

If your users do not expect a specific result immediately (eg. Inventory apps)

  • Send photos (front/nutrition/ingredients/packaging): most painless thing for your users
  • The Open Food Facts AI Robotoff will generate some derived data from the photos
  • Overtime, other apps, and the Open Food Facts community will fill the data gaps

If your users do expect a result immediately (eg Nutrition apps, Scoring apps…)

  • Send nutrition facts + category > get the Nutri-Score
  • Send ingredients > get the NOVA group (about food ultra-processing), additives, allergens, normalized ingredients, vegan, vegetarian…
  • Send category (strict minimum) + labels + origins of ingredients + packaging (photo and text) > get the Eco-Score (about environmental impact)

Open Data License

The database in under the OdBL. This means attributing the source and also contributing back any additions (photos, data), which this package makes easy to do. You can check the terms of use here : Terms of use.

Useful recourses

Contribute to the package

If found a bug or missing features in this package, please open an issue for it.

  • Issue Tracker: github.com/openfoodfacts/openfoodfacts-dart/issues
  • Source Code: github.com/openfoodfacts/openfoodfacts-dart

Support

If you are having issues, that go beyond the scope of this package, please write to us on Slack or send us an email at contact@openfoodfacts.org

Testing

Execute the following command from the root of the repository to run the tests:

dart test

Applications using this SDK

Official application

Open Food Facts (Codename Smoothie) is the official app developed by Open Food Facts, which is available on Android and iOS. The source code is also available on GitHub.

Third party applications

Feel free to open a PR to add your application in this list.

Authors

Thanks to Alexander Schacht and Primaël Quémerais for the initial creation of this package.

Contributors

Drag Racing

Libraries

src/utils/abstract_query_configuration
src/model/additives
src/model/agribalyse
src/model/allergens
src/model/parameter/allergens_parameter
src/model/attribute
src/model/attribute_group
src/utils/autocomplete_manager
src/search/autocomplete_search_result
src/search/autocomplete_single_result
src/utils/autocompleter
src/personalized_search/available_attribute_groups
src/personalized_search/available_preference_importances
src/personalized_search/available_product_preferences
src/model/badge_base
src/model/parameter/barcode_parameter
src/utils/barcodes_validator
src/model/parameter/bool_map_parameter
src/utils/country_helper
src/prices/currency
src/model/ecoscore_adjustments
src/model/ecoscore_data
src/events
src/model/events_base
src/folksonomy
src/search/fuzziness
src/prices/get_locations_order
src/prices/get_locations_parameters
src/prices/get_locations_result
src/prices/get_parameters_helper
src/prices/get_prices_order
src/prices/get_prices_parameters
src/prices/get_prices_result
src/utils/http_helper
src/utils/image_helper
src/model/ingredient
src/model/parameter/ingredients_analysis_parameter
src/model/ingredients_analysis_tags
src/model/insight
src/utils/invalid_barcodes
src/utils/json_helper
src/interface/json_object
src/model/key_stats
src/model/knowledge_panel
src/model/knowledge_panel_action
src/model/knowledge_panel_element
src/model/knowledge_panels
src/utils/language_helper
src/model/leaderboard_entry
src/model/localized_tag
src/prices/location
src/prices/location_osm_type
src/model/login_status
src/personalized_search/matched_product
src/personalized_search/matched_product_v2
src/prices/maybe_error
src/model/nutrient
src/model/nutrient_levels
src/model/nutriments
src/utils/nutriments_helper
src/utils/ocr_field
src/model/ocr_ingredients_result
src/model/ocr_packaging_result
src/model/off_tagged
src/model/old_product_result
src/open_food_api_client
src/utils/open_food_api_configuration
src/open_food_search_api_client
src/open_prices_api_client
openfoodfacts
src/prices/order_by
src/model/ordered_nutrient
src/model/ordered_nutrients
src/model/origins_of_ingredients
src/model/packaging
src/model/parameter/page_number
src/model/parameter/page_size
src/interface/parameter
src/model/per_size
src/model/parameter/pnns_group2_filter
src/utils/pnns_groups
src/personalized_search/preference_importance
src/prices/price
src/prices/price_per
src/prices/price_product
src/model/product
src/utils/product_fields
src/model/product_freshness
src/utils/product_helper
src/model/product_image
src/model/product_list
src/model/product_packaging
src/personalized_search/product_preferences_manager
src/personalized_search/product_preferences_selection
src/utils/product_query_configurations
src/model/product_result_field
src/model/product_result_field_answer
src/model/product_result_v3
src/utils/product_search_query_configuration
src/model/product_state
src/model/product_stats
src/model/product_tag
src/prices/proof
src/prices/proof_type
src/robot_off_api_client
src/model/robotoff_question
src/model/robotoff_question_order
src/model/search_result
src/model/parameter/search_terms
src/model/send_image
src/utils/server_type
src/prices/session
src/model/sign_up_status
src/model/parameter/sort_by
src/model/spelling_corrections
src/model/parameter/states_tags_parameter
src/model/status
src/utils/suggestion_manager
src/model/parameter/tag_filter
src/model/tag_i18n
src/utils/tag_type
src/utils/tag_type_autocompleter
src/model/taxonomy_additive
src/model/taxonomy_allergen
src/model/taxonomy_category
src/model/taxonomy_country
src/model/taxonomy_ingredient
src/model/taxonomy_label
src/model/taxonomy_language
src/search/taxonomy_name
src/search/taxonomy_name_autocompleter
src/model/taxonomy_nova
src/model/taxonomy_origin
src/model/taxonomy_packaging
src/model/taxonomy_packaging_material
src/model/taxonomy_packaging_recycling
src/model/taxonomy_packaging_shape
src/utils/taxonomy_query_configuration
src/utils/unit_helper
src/utils/uri_helper
src/utils/uri_reader
src/utils/uri_reader_stub
src/model/user
src/model/user_agent
src/model/parameter/without_additives