Home > Mobile >  _CastError (type 'String' is not a subtype of type 'Map<String, dynamic>'
_CastError (type 'String' is not a subtype of type 'Map<String, dynamic>'

Time:12-29

this is the code where I get an exception

I tried so many solution but did not work.

CodePudding user response:

Try this

    Future<void> fetchAndsetProduct() async {
        final url = Uri.parse(
            'https://shoppingapp-ef04b-default-rtdb.firebaseio.com/product.json');
        try {
          final response = await http.get(url);
          Map<String, dynamic> extractedData = json.decode(response.body);

          if (extractedData == null || extractedData["error"] == "Permission denied") {
            return;
          }

          final List<Product> loadedProducts = [];
          extractedData.forEach((prodId, prodData) {
          loadedProducts.add(Product(
           id: prodId,
           title: prodData['title'],
           description: prodData['description'],
           price: prodData['price'],
           isFavorite: prodData['isFavorite'],
           imageUrl: prodData['imageUrl'],
        ));
      });
        _items = loadedProducts;
         notifyListeners();
      } catch (error) {
         throw (error);
      }
   }
  • Related