convertFieldsToStrings function

List<String> convertFieldsToStrings(
  1. List<ProductField> fields,
  2. List<OpenFoodFactsLanguage> languages
)

NOTE: if one of the fields is IN_LANGUAGES and languages is empty - the function will throw.

Implementation

List<String> convertFieldsToStrings(
    List<ProductField> fields, List<OpenFoodFactsLanguage> languages) {
  final fieldsStrings = <String>[];

  const fieldsInLanguages = [
    ProductField.CATEGORIES_TAGS_IN_LANGUAGES,
    ProductField.LABELS_TAGS_IN_LANGUAGES,
    ProductField.NAME_IN_LANGUAGES,
    ProductField.COUNTRIES_TAGS_IN_LANGUAGES,
    ProductField.INGREDIENTS_TEXT_IN_LANGUAGES,
    ProductField.PACKAGING_TEXT_IN_LANGUAGES,
    ProductField.INGREDIENTS_TAGS_IN_LANGUAGES,
    ProductField.IMAGES_FRESHNESS_IN_LANGUAGES,
  ];

  for (final field in fields) {
    if (fieldsInLanguages.contains(field)) {
      if (languages.isEmpty) {
        throw ArgumentError(
            'Cannot request in-lang field $field without language');
      }
      for (final language in languages) {
        fieldsStrings.add('${field.offTag}${language.offTag}');
      }
    } else {
      fieldsStrings.add(field.offTag);
    }
  }

  return fieldsStrings;
}