diff --git a/lib/src/open_food_api_client.dart b/lib/src/open_food_api_client.dart index b360b9a5da..1fa0f65a1e 100644 --- a/lib/src/open_food_api_client.dart +++ b/lib/src/open_food_api_client.dart @@ -1084,14 +1084,14 @@ class OpenFoodAPIClient { /// print(orderedNutrients.nutrients[10].name); // Vitamin A /// ``` static Future getOrderedNutrients({ - required final String? cc, + required final OpenFoodFactsCountry country, required final OpenFoodFactsLanguage language, final QueryType? queryType, }) async => OrderedNutrients.fromJson( HttpHelper().jsonDecode( await getOrderedNutrientsJsonString( - country: CountryHelper.fromJson(cc)!, + country: country, language: language, ), ), diff --git a/lib/src/utils/country_helper.dart b/lib/src/utils/country_helper.dart index f5053640d9..9110cff516 100644 --- a/lib/src/utils/country_helper.dart +++ b/lib/src/utils/country_helper.dart @@ -761,20 +761,12 @@ enum OpenFoodFactsCountry implements OffTagged { final String offTag; /// Returns the first [OpenFoodFactsCountry] that matches the [offTag]. - static OpenFoodFactsCountry? fromOffTag(final String? offTag) => - OffTagged.fromOffTag(offTag, OpenFoodFactsCountry.values) - as OpenFoodFactsCountry?; -} - -/// Helper class around [OpenFoodFactsCountry] -class CountryHelper { - /// Converts an ISO 2 code into an [OpenFoodFactsCountry] (case-insensitive). - /// - /// E.g. 'fr' and 'FR' will give the same result: OpenFoodFactsCountry.FRANCE. - static OpenFoodFactsCountry? fromJson(String? code) { - if (code == null) { - return null; + static OpenFoodFactsCountry? fromOffTag(final String? offTag) { + // special case as we use 'uk' in off-dart + if (offTag == 'gb') { + return OpenFoodFactsCountry.UNITED_KINGDOM; } - return OpenFoodFactsCountry.fromOffTag(code.toLowerCase()); + return OffTagged.fromOffTag(offTag, OpenFoodFactsCountry.values) + as OpenFoodFactsCountry?; } } diff --git a/test/ordered_nutrient_test.dart b/test/ordered_nutrient_test.dart index 83e5250156..0396320d51 100644 --- a/test/ordered_nutrient_test.dart +++ b/test/ordered_nutrient_test.dart @@ -164,7 +164,7 @@ void main() { for (final OpenFoodFactsCountry country in countries) { checkNutrients( await OpenFoodAPIClient.getOrderedNutrients( - cc: country.offTag, + country: country, language: language, ), country, @@ -191,12 +191,16 @@ void main() { OpenFoodFactsLanguage.ENGLISH: 'Energy', OpenFoodFactsLanguage.PORTUGUESE: 'Energia', }; - const Set countries = {'us', 'it', 'br'}; + const Set countries = { + OpenFoodFactsCountry.USA, + OpenFoodFactsCountry.ITALY, + OpenFoodFactsCountry.BRAZIL, + }; for (final OpenFoodFactsLanguage language in energies.keys) { - for (final String country in countries) { + for (final OpenFoodFactsCountry country in countries) { final OrderedNutrients orderedNutrients = await OpenFoodAPIClient.getOrderedNutrients( - cc: country, + country: country, language: language, ); final OrderedNutrient? found =