gpt4 book ai didi

flutter - 将对象数组拆分为多个列表

转载 作者:行者123 更新时间:2023-12-03 03:58:20 45 4
gpt4 key购买 nike

这是我的对象列表

[{id: 3, price: 77}, {id: 2, price: 66}]

什么是从上一个列表中提取此列表 [77,66]的最干净方法

最佳答案

如果是json数组,则可以使用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']));
}

map()将遍历数组的所有元素,我们将仅提取您所请求的价格并将其放入另一个列表中并返回。
您可以从 https://flutter.dev/docs/cookbook/networking/background-parsing获取引用

关于flutter - 将对象数组拆分为多个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59410840/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com