Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Data from JSON Array in Flutter/Dart

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>();
}
like image 793
Asad Ali Avatar asked Sep 03 '25 15:09

Asad Ali


1 Answers

This should do what you want:

var intIds = ids.map<int>((m) => m['id'] as int).toList();
like image 181
Günter Zöchbauer Avatar answered Sep 05 '25 03:09

Günter Zöchbauer