I am a beginner in Flutter and I am stuck at converting my API fetched data to my custom model. I am using an API that provides me with this data:
{"meals":[{"idMeal":"52852","strMeal":"Tuna Nicoise","strDrinkAlternate":null,"strCategory":"Seafood",
"strArea":"French","strInstructions":"Heat oven to 200C\/fan 180C\/gas 6. Toss the potatoes with 2 tsp oil and some seasoning. Tip onto a large baking tray, then roast for 20 mins, stirring halfway, until crisp, golden and cooked through.\r\nMeanwhile, put eggs in a small pan of water, bring to the boil, then simmer for 8-10 mins, depending on how you like them cooked. Plunge into a bowl of cold water to cool for a few mins. Peel away the shells, then cut into halves.\r\nIn a large salad bowl, whisk together the remaining oil, red wine vinegar, capers and chopped tomatoes. Season, tip in the onion, spinach, tuna and potatoes, then gently toss together. Top with the eggs, then serve straight away.",
"strMealThumb":"https:\/\/www.themealdb.com\/images\/media\/meals\/yypwwq1511304979.jpg","strTags":null,
"strYoutube":"https:\/\/www.youtube.com\/watch?v=3_UAxkx0u6U",
"strIngredient1":"Potatoes","strIngredient2":"Olive Oil","strIngredient3":"Eggs","strIngredient4":"Red Wine Vinegar","strIngredient5":"Capers","strIngredient6":"Sunflower Oil","strIngredient7":"Red Onions","strIngredient8":"Spinach","strIngredient9":"Tuna","strIngredient10":"","strIngredient11":"","strIngredient12":"","strIngredient13":"","strIngredient14":"","strIngredient15":"","strIngredient16":"","strIngredient17":"","strIngredient18":"","strIngredient19":"","strIngredient20":"",
"strMeasure1":"450g","strMeasure2":"2 tblsp ","strMeasure3":"4","strMeasure4":"1 tbls","strMeasure5":"2 tblsp ","strMeasure6":"50g","strMeasure7":"\u00bd","strMeasure8":"100g ","strMeasure9":"400g","strMeasure10":"","strMeasure11":"","strMeasure12":"","strMeasure13":"","strMeasure14":"","strMeasure15":"","strMeasure16":"","strMeasure17":"","strMeasure18":"","strMeasure19":"","strMeasure20":"",
"strSource":"https:\/\/www.bbcgoodfood.com\/recipes\/9529\/winter-tuna-nioise",
"strImageSource":null,"strCreativeCommonsConfirmed":null,"dateModified":null}]}
I want to store all the values of keys that begin with "strIngredient" into a list, and the same goes for "strMeasure"
But I haven't come across a method yet that could do the mentioned.
You can get all keys
and then check each one against your condition.
Map<String,String> meals= api_data['meals'][0];
List<String> allKeys = meals.keys.toList();
for(String key in allKeys){
if(key.startsWith("strIngredient")){
print(meals[key]);
}
}