searchProducts static method
Future<SearchResult>
searchProducts(
- User? user,
- AbstractQueryConfiguration configuration, {
- UriProductHelper uriHelper = uriHelperFoodProd,
Search the OpenFoodFacts product database with the given parameters. Returns the list of products as SearchResult. Query the language specific host from OpenFoodFacts.
ProductSearchQueryConfiguration configuration =
ProductSearchQueryConfiguration(
parametersList: <Parameter>[
...(insert parameter's from below here)
],
);
SearchResult result = await OpenFoodAPIClient.searchProducts(
User(userId: '', password: ''),
configuration,
);
print(result.products?[0].productName);
Possible parameters are:
By name:
SearchTerms(terms: ['Nutella']),
Without additives:
WithoutAdditives()
By brand, stores, ingredients, other TagFilterType:
TagFilter.fromType(
tagFilterType: TagFilterType.BRANDS,
tagName: 'Coca-cola',
),
By category (PnnsGroup2):
PnnsGroup2Filter(pnnsGroup2: PnnsGroup2.POTATOES),
By VeganStatus, VegetarianStatus or/and PalmOilFreeStatus:
IngredientsAnalysisParameter(veganStatus: VeganStatus.VEGAN),
You can sort the SortOption criteria:
SortBy(option: SortOption.POPULARITY),
Limit the amount of products per query
PageSize(size: 5),
ยดยดยด
And query more products
PageNumber(page: 2),
-------
Search for products related to a user
```dart
final ProductSearchQueryConfiguration configuration =
ProductSearchQueryConfiguration(
parametersList: [
TagFilter.fromType(tagFilterType: type, tagName: userId),
],
language: language,
fields: [
ProductField.BARCODE,
ProductField.STATES_TAGS,
],
version: ProductQueryVersion.v3,
);
Where type is:
Implementation
static Future<SearchResult> searchProducts(
final User? user,
final AbstractQueryConfiguration configuration, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Response response = await configuration.getResponse(user, uriHelper);
final String jsonStr = _replaceQuotes(response.body);
final SearchResult result = SearchResult.fromJson(
HttpHelper().jsonDecode(jsonStr),
);
_removeImages(result, configuration);
return result;
}