Trying to use firebase for a get request for my flutter app and getting error

Viewed 300

enter image description here[This is the front screen which is a gridTile and getting data from firebase

<!-- this is the method which the grid time is using to fetch data from firebase-->

 Future<void> fetchAndSetProducts() async {
    const url ='MY flutter url';
    try {
      final response = await http.get(url);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;
      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);
    }
  }

]2

enter image description here

1 Answers

This is your json:

{
   "-M1We9LYxWhijvoECbpY":{
      "description":"Abe Kya be saale",
      "imageURL":"https://cdn.wallpapersafari.com/89/2/Lnxobm.jpg",
      "isFavorite":false,
      "price":747.0,
      "title":"test"
   },
   "-M1WlRlzv8rYwhGklkyc":{
      "description":"Abe sale nikal na",
      "imageURL":"https://cdn.wallpapersafari.com/89/2/Lnxobm.jpg",
      "isFavorite":false,
      "price":43.0,
      "title":"fdddfff"
   },
   "-M1XmbG11YGDgSdJyfvm":{
      "description":"dfvdfvfdvfvfdb",
      "imageURL":"https://cdn-products.eneba.com/resized-products/qoA2QojrFaqg8VtfKSEJm1jsXU-9UEJJSNf2WTvMBjE_390x400_2x-0.jpeg",
      "isFavorite":false,
      "price":74.0,
      "title":"new"
   },
   "-M1axCdqSDIIijYYSXDX":{
      "description":"gctdcgchvhgvggh",
      "imageURL":"https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg",
      "isFavorite":false,
      "price":411.0,
      "title":"bhbn"
   },
   "-M1i_KbskfnTLxfFRIiu":{
      "description":"dzvfvbvfbvggfbvxfgb gfvb ",
      "imageUrl":"https://5.imimg.com/data5/IJ/JK/MY-11744895/playing-football-500x500.jpg",
      "isFavorite":false,
      "price":65.0,
      "title":"fbvfbvc cv"
   }
}

All the nodes contain the attribute imageURL except one node that contains the attribute imageUrl. So first you need to change your json so all nodes will contain imageURL (capital URL), and then in your code do the following:

  extractedData.forEach((prodId, prodData) {
        loadedProducts.add(Product(
          id: prodId,
          title: prodData['title'],
          description: prodData['description'],
          price: prodData['price'],
          isFavorite: prodData['isFavorite'],
          imageUrl: prodData['imageURL'],
        ));
      });
Related