0
Future<void> fetchAndSetProducts() async {
final url = Uri.https('flutter-project-c43e5-default-rtdb.firebaseio.com',
    '/products.json?auth=$authToken');
try {
  final response = await http.get(url);
  final extractedData = json.decode(response.body) as Map<String, dynamic>;
  if (extractedData == null) {
    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);
}

}

I am getting this error Unhandled Exception: FormatException: Unexpected character (at character 1) not found I have tried everything and I am not using dio Can someone tell what is wrong?

Naman Arya
  • 1
  • 1
  • 2
  • Does this answer your question? [Flutter FormatException: Unexpected character (at character 1)](https://stackoverflow.com/questions/55671441/flutter-formatexception-unexpected-character-at-character-1) – DEFL Apr 19 '22 at 14:24
  • You didn't check `response.statusCode`, so likely you're trying to parse a failure page that isn't JSON. – jamesdlin Apr 19 '22 at 16:12

1 Answers1

0

why do you have a , in your url?:

Future<void> fetchAndSetProducts() async {
final url = Uri.https('flutter-project-c43e5-default-rtdb.firebaseio.com',
    '/products.json?auth=$authToken');
...

change this block to:

Future<void> fetchAndSetProducts() async {
final url = Uri.https('flutter-project-c43e5-default-rtdb.firebaseio.com/products.json?auth=$authToken');
...
nonsocchi
  • 84
  • 2
  • 6