Reputation: 663
This is my object list
[{id: 3, price: 77}, {id: 2, price: 66}]
What is the cleanest way to extract this list [77,66]
from the previous list
Upvotes: 2
Views: 2204
Reputation: 1969
In case of json arrays you can use map()
import 'dart:convert';
List<int> convertToList(){
String response=[{id: 3, price: 77}, {id: 2, price: 66}];//Plain String
var json=json.decode(response);//Convert it to json object
return List<int>.from(str.map((item) =>item['price']));
}
The map() will iterate through all the elements of the array and we will extract only the price as you have requested and put it in another list and return it. You can get reference from https://flutter.dev/docs/cookbook/networking/background-parsing
Upvotes: 1