Mohamed Shaheen
Mohamed Shaheen

Reputation: 663

split array of objects to multiple lists

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

Answers (1)

Guru Prasad mohapatra
Guru Prasad mohapatra

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

Related Questions