I am basically from JS background and now trying my hands on flutter framework. I am stuck in formatting my data I received from API.
Data Received an array of objects
[
{
"id": 1,
"name": "name1",
"type": "Both",
"count": 4
},
{
"id": 2,
"name": "name2",
"type": "Both",
"count": 6
},
{
"id": 3,
"name": "name1",
"type": "Both",
"count": 2
},
{
"id": 4,
"name": "name3",
"type": "Both",
"count": 8
},
......
]
My Requirement is I will have to group the data based on the name
{
name1: [
{
"id": 1, "name":"name1", "type": "Both", "count": 4
},
{
"id": 3, "name":"name1", "type": "Both", "count": 2
}
],
name2: [
{
"id": 2, "name":"name2", "type": "Both", "count": 6
}
],
.....
}
In dart, I have managed to do that grouping using groupBy from collections.dart package.
The problem is I cannot loop through the grouped data. I need to access the count for some manipulation for each grouped name.
Below is the code snipped which I tried
Map data;
List partySnapData;
Map newMap;
Future _getTopParties() async {
final response =await http.get(API_URL + '/getPartySnapShot');
data =json.decode(response.body);
partySnapData = data['resultObj'];
setState(() {
newMap = groupBy(partySnapData, (obj) => obj['party_name']);
for(var v in newMap.values) {
print(v);
}
});
}
The print(v) actually gave me the value mapped for the map E.G
[
{ "id": 1, "name":"name1", "type": "Both", "count": 4 },
{ "id": 3, "name":"name1", "type": "Both", "count": 2 }
],
.....,
......
Now, how do I loop or iterate through the array, here the v, so that i can access the elements inside?
I found a solution to this. I am posting that, incase it helps someone.
I wanted to loop through the v to generate objects.