''List<dynamic>' is not a subtype of type 'List<String>' when decoding data
When I try to get data from my Firebase database I encounter this error when decoding List. This is the error. Unhandled Exception: type 'List' is not a subtype of type 'List'. I attach the method, which triggers the error.
Future<void> fetchProducts() async {
final url = Uri.parse(
'https://recipier-e1139-default-rtdb.europe-west1.firebasedatabase.app/recipes.json');
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
if (extractedData.isEmpty || extractedData['error'] != null) {
return;
}
List<Recipe> loadedRecipes = [];
extractedData.forEach(
(id, data) {
loadedRecipes.add(
Recipe(
title: data['title'],
description: data['description'],
id: id,
ingredients: data['ingredients'],
steps: data['steps'],
kcal: data['kcal'],
p: data['p'],
c: data['c'],
f: data['f'],
),
);
},
);
_recipes = loadedRecipes;
notifyListeners();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
