I'm trying to render a list of elements in Flutter and the list data is coming from a REST API. However, I'm having trouble understanding how to parse this data correctly. I want the parsed data to look like this:
['Chicken roll', 'Fresh salads', 'Wrape', 'Pizza', 'Fried Chicken', 'Veggie', 'Burgers']
This is the JSON object:
{
"status":true,
"error_message":"",
"success_message":"Task completed successfully.",
"data":[
{
"id":1064,
"name":"Chicken roll"
},
{
"id":1061,
"name":"Fresh salads"
},
{
"id":1059,
"name":"Wrape"
},
{
"id":1057,
"name":"Pizza"
},
{
"id":1055,
"name":"Fried chicken"
},
{
"id":1053,
"name":"Veggie"
},
{
"id":1051,
"name":"Burgers"
}
]
}
How do I achieve this? My current code is as follows:
var _categoryResponse;
var _categoryData;
var categoryData;
List<String> allCategories;
Future fetchCategories() async {
_categoryResponse = await http.get(_categories);
_categoryResponse = jsonDecode(_categoryResponse.body);
_categoryData = _categoryResponse['data'].toString();
categoryData = categoryDataFromJson(_categoryData);
}