[英]How can I filter List<Map<String, dynamic>> to get a value in Flutter?
I have a json data and I am loading into List<Map<String, dynamic>>
using await jsonDecode. 我有一个json数据,我正在使用await jsonDecode加载到List<Map<String, dynamic>>
。 . 。
Example, I have a; 例如,我有一个;
and I need to filter the son to get maturity value: 689.19 而我需要过滤儿子以获得到期价值: 689.19
Also if I have a amount = 12500 and value = 689.19 I need to get caliber which is 24. 另外,如果我的数量= 12500,值= 689.19,则需要获得24的口径。
How can I filter List> json data to get a value in Flutter? 如何过滤List> json数据以在Flutter中获取值?
[
{"amount": "5000", "caliber": "12", "maturity": "484.25"},
{"amount": "5000", "caliber": "24", "maturity": "275.67"},
{"amount": "7500", "caliber": "12", "maturity": "726.38"},
{"amount": "7500", "caliber": "24", "maturity": "413.51"},
{"amount": "10000", "caliber": "12", "maturity": "968.50"},
{"amount": "10000", "caliber": "24", "maturity": "551.35"},
{"amount": "12500", "caliber": "12", "maturity": "1210.63"},
{"amount": "12500", "caliber": "24", "maturity": "689.19"},
{"amount": "15000", "caliber": "12", "maturity": "1452.76"},
{"amount": "15000", "caliber": "24", "maturity": "827.03"},
{"amount": "15000", "caliber": "12", "maturity": "1694.89"},
{"amount": "17500", "caliber": "24", "maturity": "964.87"},
{"amount": "17500", "caliber": "36", "maturity": "727.66"},
{"amount": "17500", "caliber": "48", "maturity": "613.53"},
{"amount": "17500", "caliber": "60", "maturity": "548.44"},
{"amount": "20000", "caliber": "12", "maturity": "1937.01"},
{"amount": "20000", "caliber": "24", "maturity": "1102.71"}
]
UPDATE: With @Muldec help I finally got it. 更新:在@Muldec的帮助下,我终于明白了。
First I load above son to List as shown below. 首先,我在子上方加载到列表,如下所示。 And apply @Muldec answer and it worked. 并应用@Muldec答案,就可以了。
List<Map> myList = List<Map>.from(jsonDecode(jsonFastCalculation) as List);
final item = myList.firstWhere((e) => e['amount'] == '12500' && e['caliber'] == '24');
print(item['maturity']);
You can use the firstWhere
or where
methods on your list to get the Map
element you need or a list of Map
depending on your search criteria. 您可以使用firstWhere
或where
名单上的方法来获取Map
,你需要元素或列表Map
取决于你的搜索条件。
Supposing you are absolutely sure your search criteria will give you only one item (or that you only care of the first item meeting your criteria) here's an example of code with firstWhere
: 假设您绝对确定搜索条件只会给您一项(或者您只在乎满足条件的第一项),这是firstWhere
的代码firstWhere
:
List<Map<String, dynamic>> myList = ....;
final item = myList.firstWhere((e) => e['amount'] == '12500' && e['caliber'] == '24');
print(item['maturity']);
Finding the caliber based on the amount and the maturity is simply a mater of changing the test in the firstWhere
method 根据数量和成熟度找到口径只是在firstWhere
方法中更改测试的firstWhere
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.