I am working on a Dart application and trying to decode JSON data, but I’m encountering an error that I can’t seem to resolve.
Error:
Errore: type ‘String’ is not a subtype of type ‘Iterable’ in type cast
Flutter code:
Map value= await apiRequestMap('/api/Recipes/' + model, {}, UrlRequest.GET, true, false, true);
String decodeMapOperation = json.encode(value);
(decodeMapOperation as Iterable<dynamic>) // crash
.map((dynamic jsonObject) => print(jsonObject['name']))
.toList();
Json:
[
{
"id": 2,
"name": "salsa bolognese",
"tool": "",
"description": "",
"link": "",
"picture": "http://207.154.221.255:8600/pictures/recipes/Salsa Bolognese.jpg",
"phases": [
{
"Set_Rpm1_Utensile": "0",
"Set_Rpm2_Utensile": "0",
"On_Rpm_Utensile": "0",
"Off_Rpm_Utensile": "0",
"Tempo_Rpm_Utensile": "0",
"Set_Vuoto": "0",
"On_Vuoto": "0",
"Off_Vuoto": "0",
"Tempo_Vuoto": "0",
"Set_Pressione_In": "0",
"Set_Pressione_Out": "0",
"Spatola_On": "false",
"N_Rotazioni_Spatola": "0",
"Pausa_Spatola": "0",
"Set_Caldo": "0",
"Set_Vapore_Diretto": "0",
"On_Caldo": "0",
"Off_Caldo": "0",
"Tempo_Caldo": "0",
"Set_Freddo": "0",
"Set_Gelida": "0",
"On_Freddo": "0",
"Off_Freddo": "0",
"Tempo_Freddo": "0"
}
],
"time": 12,
"instructions": [
{
"id": 1,
"content": "Inserire tutti gli ingredienti nel Qbo",
"order": 1,
"recipeId": 2
},
{
"id": 2,
"content": "Chiudere il coperchio",
"order": 2,
"recipeId": 2
},
{
"id": 3,
"content": "Inclinare la vasca a livello 2 e avviare il processo",
"order": 3,
"recipeId": 2
},
{
"id": 4,
"content": "Cucinare la salsa in pressione (fase 2 e 3)",
"order": 4,
"recipeId": 2
},
{
"id": 5,
"content": "Evaporare la parte di liquido in eccesso (fase 4)",
"order": 5,
"recipeId": 2
},
{
"id": 6,
"content": "Raddrizzare la vasca e ripristinare la pressione aprendo la tramoggia",
"order": 6,
"recipeId": 2
},
{
"id": 7,
"content": "Scaricare il prodotto",
"order": 7,
"recipeId": 2
}
],
"ingredients": [
{
"id": 1,
"name": "MACINATO DI BOVINO",
"quantity": "650 gr",
"recipeId": 2
},
{
"id": 2,
"name": "MACINATO DI SUINO",
"quantity": "450 gr",
"recipeId": 2
},
{
"id": 3,
"name": "PANCETTA MACINATA",
"quantity": "450 gr",
"recipeId": 2
},
{
"id": 4,
"name": "SOFFRITTO",
"quantity": "600 gr",
"recipeId": 2
},
{
"id": 5,
"name": "TRIPLO CONCENTRATO DI POMODORO",
"quantity": "225 gr",
"recipeId": 2
},
{
"id": 6,
"name": "VINO BIANCO",
"quantity": "100 gr",
"recipeId": 2
},
{
"id": 7,
"name": "SALE",
"quantity": "12 gr",
"recipeId": 2
},
{
"id": 8,
"name": "PEPE",
"quantity": "3 gr",
"recipeId": 2
},
{
"id": 9,
"name": "ALLORO 1 FOGLIA",
"quantity": "1",
"recipeId": 2
},
{
"id": 10,
"name": "NOCE MOSCATA",
"quantity": "1",
"recipeId": 2
}
],
"updatedAt": "2023-10-09T14:39:18.661132Z"
}
]
2
Answers
from line
Use
json.decode
insteadIf
value
is a json stringified, after you decode it would not be a string, so do not type it as StringNow you can infer this as an Iterable since you know that is an Iterable.
I like to map like this: