selectedImagesFromJson static method

List<ProductImage>? selectedImagesFromJson(
  1. Map? json
)

Returns ProductImages from a product JSON map for "Selected images"

Implementation

static List<ProductImage>? selectedImagesFromJson(Map? json) {
  if (json == null) return null;

  var imageList = <ProductImage>[];
  for (var field in ImageField.values) {
    for (var size in ImageSize.values) {
      for (OpenFoodFactsLanguage lang in OpenFoodFactsLanguage.values) {
        // use the field to get the size
        if (json[field.offTag] == null) {
          continue;
        }
        var sizeJson = json[field.offTag] as Map<String, dynamic>?;

        // use the size to get the language
        if (sizeJson == null) {
          continue;
        }
        var langJson = sizeJson[size.offTag] as Map<String, dynamic>?;

        // use the language to get the url
        if (langJson == null) {
          continue;
        }
        var url = langJson[lang.offTag] as String?;

        // use the url to build the image
        if (url == null) {
          continue;
        }
        var image = ProductImage(
          field: field,
          size: size,
          language: lang,
          url: url,
        );

        imageList.add(image);
      }
    }
  }
  return imageList;
}