Extract Data from JSON Array in Flutter/Dart
7,381
Solution 1
This should do what you want:
var intIds = ids.map<int>((m) => m['id'] as int).toList();
Solution 2
This is what I did when got "array of object" as response (I know I'm late. But it will help the next guy)
List list = json.decode(response.body);
if (response.body.contains("id")) {
var lst = new List(list.length);
for (int i = 0; i < list.length; i++) {
var idValue = list[i]['id'];
print(idValuee);
}
Author by
Asad Ali
Updated on December 07, 2022Comments
-
Asad Ali over 1 year
This is the response from request.
var response = [{"id":4731},{"id":4566},{"id":4336},{"id":4333},{"id":4172},{"id":4170},{"id":4168},{"id":4166},{"id":4163},{"id":4161}];
How to extract ids and store in List of int using flutter. I have try this code but not working.
Future<List<int>> fetchTopIds() async{ final response = await client.get('$_baseUrl/posts?fields=id'); final ids = json.decode(response.body); return ids.cast<int>(); }