'Unhandled Exception: FormatException: Unexpected character (at character 1) <asynchronous suspension>

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?



Solution 1:[1]

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');
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1