I'm new to flutter and am trying to get data into a list view in my app.
I have this json object structure retrieved from a webserver:
{
"item_data1":
{
"index": 0,
"age": 39,
"picture": "http://placehold.it/32x32",
"name": "Sospeter",
"email": "[email protected]"
},
"item_data2":
{
"index": 1,
"age": 49,
"picture": "http://placehold.it/32x32",
"name": "Eric",
"email": "[email protected]"
}
}
NB: The keys "item_data1" and "item_data2" are dynamic and cannot be pre-determined in code.
You may use the following link to get the json: http://www.json-generator.com/api/json/get/bUAhSFmhNK?indent=2
My intention is to dart:convert the json in my project.
So far I have this:
Future<List<User>> _getUsers() async {
var url = "http://www.json-generator.com/api/json/get/bUAhSFmhNK?indent=2"; //Obj
var data = await http.get(url);
var jsonData = json.decode(data.body);
//print(jsonData.toString()); <-- returns data
List<User> users = [];
for(var u in jsonData){ <!-- breaks
User user = User(u["index"], u["age"], u["name"], u["email"], u["picture"]);
users.add(user);
}
//print(users.length);
return users;
}
How can I safely get the "name", "email",...etc?
I'm stumped. I'll appreciate any help I can get :)