Unhandled Exception: FormatException: Unexpected character

Viewed 945

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

2 Answers

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 {
    ...
  }
}

I found it Please change HTML file Json by simply

final url =
            Uri.https('shopda-83b00-default-rtdb.firebaseio.com', '/products.json');
Related