Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProductPackaging fields can be a JSON map or a string. #977

Closed
Rujitsuki opened this issue Sep 27, 2024 · 3 comments
Closed

ProductPackaging fields can be a JSON map or a string. #977

Rujitsuki opened this issue Sep 27, 2024 · 3 comments

Comments

@Rujitsuki
Copy link

Description

When searching for products, the error _TypeError (type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast) occasionally occurs. Apparently, in the ProductPackage model, the fields 'shape' and 'material' can either be a JSON map or a string.

Expected behavior

In the response example 1 of the API documentation, 'shape', 'material' and 'recycling' appear twice, once as a string and once as a key-value pair. (Documentation)

    OpenFoodAPIConfiguration.userAgent = UserAgent(name: 'MyApp');
    OpenFoodAPIConfiguration.globalLanguages = <OpenFoodFactsLanguage>[
      OpenFoodFactsLanguage.ENGLISH
    ];
    OpenFoodAPIConfiguration.globalCountry = OpenFoodFactsCountry.GERMANY;

    ProductSearchQueryConfiguration searchQueryConfiguration =
        ProductSearchQueryConfiguration(
      parametersList: [
        SearchTerms(terms: [query])
      ],
      version: const ProductQueryVersion(2),
    );

    SearchResult result = await OpenFoodAPIClient.searchProducts(
        OpenFoodAPIConfiguration.globalUser, searchQueryConfiguration);

Stacktraces

E/flutter (12732): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast
E/flutter (12732): #0      _$ProductPackagingFromJson (package:openfoodfacts/src/model/product_packaging.g.dart:13:49)
E/flutter (12732): #1      new ProductPackaging.fromJson (package:openfoodfacts/src/model/product_packaging.dart:62:7)
E/flutter (12732): #2      MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31)
E/flutter (12732): #3      ListIterator.moveNext (dart:_internal/iterable.dart:354:26)
E/flutter (12732): #4      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
E/flutter (12732): #5      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
E/flutter (12732): #6      new List.of (dart:core-patch/array_patch.dart:39:18)
E/flutter (12732): #7      ListIterable.toList (dart:_internal/iterable.dart:224:7)
E/flutter (12732): #8      _$ProductFromJson (package:openfoodfacts/src/model/product.g.dart:133:12)
E/flutter (12732): #9      new Product.fromJson (package:openfoodfacts/src/model/product.dart:645:28)
E/flutter (12732): #10     _$SearchResultFromJson.<anonymous closure> (package:openfoodfacts/src/model/search_result.g.dart:16:32)
E/flutter (12732): #11     MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31)
E/flutter (12732): #12     ListIterator.moveNext (dart:_internal/iterable.dart:354:26)
E/flutter (12732): #13     new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
E/flutter (12732): #14     new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
E/flutter (12732): #15     new List.of (dart:core-patch/array_patch.dart:39:18)
E/flutter (12732): #16     ListIterable.toList (dart:_internal/iterable.dart:224:7)
E/flutter (12732): #17     _$SearchResultFromJson (package:openfoodfacts/src/model/search_result.g.dart:17:12)
E/flutter (12732): #18     new SearchResult.fromJson (package:openfoodfacts/src/model/search_result.dart:37:7)
E/flutter (12732): #19     OpenFoodAPIClient.searchProducts (package:openfoodfacts/src/open_food_api_client.dart:526:46)

Package information

Open Food Facts package version: 3.15.0

Hopefully, I'm not misinterpreting this :)

Rujitsuki added a commit to Rujitsuki/openfoodfacts-dart that referenced this issue Sep 27, 2024
@monsieurtanuki
Copy link
Contributor

Hi @Rujitsuki!
I would recommend using ProductQueryVersion.v3 if you need packaging info: they are not really supported in v2.
Alternate solution: if actually you don't care about packaging and want to stay in v2, please list the fields you actually need instead of the default "all fields" option.

@Rujitsuki
Copy link
Author

Hi @monsieurtanuki , thanks for the support! A selection of fields helped! The packaging wasn't important to me, and I'm now using ProductQueryVersion.v3. I was surprised that the queries stopped, so I didn't even think about setting fixed fields at first. I think I can better understand what to focus on now. Thanks again!

@monsieurtanuki
Copy link
Contributor

You're welcome @Rujitsuki!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants