2

I couldn't transform my fetched the data,

But no errors showing, I have added a print statement to find error where it occurs

   Future<void> fetchAndSetProduct() async {
        final url =
            Uri.https('shopda-83b00-default-rtdb.firebaseio.com', '/products');
        try {
          print("karan  oneonofne ");
          final response = await http.get(url);
          final extractData = json.decode(response.body) as Map<String, dynamic>;
          final List<Product> loadedProduct = [];
          extractData.forEach((prodId, prodData) {
            loadedProduct.add(Product(
                id: prodId,
                title: prodData['title'],
                description: prodData['description'],
                price: prodData['price'],
                isFavourite: prodData['isFavourite'],
                imageUrl: prodData['imageUrl']));
          });
          print(loadedProduct[1]);
          _items = loadedProduct;
          notifyListeners();
        } catch (error) {
          throw (error);
        } finally {
          print('object');
        }
      }

enter image description here

then I changed like this enter image description here

Still, I couldn't get data.I think I couldn't change HTML to Jason

4
  • 1
    There is something that went wrong with your API. It returns html Commented Aug 18, 2021 at 15:40
  • You expect some kind of objects while you got something else. Commented Aug 18, 2021 at 15:46
  • It's likely that your API call was malformed, so instead of receiving a JSON response, you received an HTML error page. Instead of using Uri.https, I recommend using Uri.parse with an URL that you've tested with a browser. Commented Aug 18, 2021 at 20:53
  • It doesnt work .could you help me and rewrite the code. it gives HTML instead of Json Commented Aug 19, 2021 at 3:15

2 Answers 2

1

I found it Please change HTML file Json by simply

final url =
            Uri.https('shopda-83b00-default-rtdb.firebaseio.com', '/products.json');
Sign up to request clarification or add additional context in comments.

Comments

0

You're trying to fetch data from your Realtime Database by using the direct link.

This requires you to sign in and that is the HTML page it returns.

You should use the firebase_database package to fetch the data from the database.

You should update your fetchAndSetProduct method to this:

Future<void> fetchAndSetProduct() async { 
  try {
    ...

    final DataSnapshot dataSnapshot =  await FirebaseDatabase.instance.reference().child('products').once();
    final extractData = json.decode(dataSnapshot.value) as Map<String, dynamic>;
    ...
  } catch (error) {
    ...
  } finally {
    ...
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.