简体   繁体   English

如何过滤列表 <Map<String, dynamic> &gt;获得Flutter的价值?

[英]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; 例如,我有一个;

  1. amount = 12500 数量= 12500
  2. caliber = 24 口径= 24

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. 您可以使用firstWherewhere名单上的方法来获取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.

相关问题 如何过滤列表<list<map<string, dynamic> &gt;&gt; 获取 Flutter 中的值? </list<map<string,> - How can I filter List<List<Map<String, dynamic>>> to get a value in Flutter? Flutter - 如何更新列表<map></map> - Flutter - How can I update a List<Map> 'Map 类型的值<string, object> ' 不能分配给 'HashMap 类型的变量<string, dynamic> ' flutter</string,></string,> - A value of type 'Map<String, Object>' can't be assigned to a variable of type 'HashMap<String, dynamic>' flutter Flutter - 如何过滤列表到 map 扩展块 - Flutter - How to filter list to map expansiontile 未处理的异常:键入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39; 在颤动中如何像这样发布 json obj - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in flutter how to post json obj like this 如何转换列表<map<string,dynamic> 到 Dart 中的单个变量? </map<string,dynamic> - How can I convert a List<Map<String,dynamic> to a single variable in Dart? 如何在 flutter 的共享优先权中存储列表 map object - How can I store list map object in share preference in flutter Flutter 如何在深层链接(动态链接)中传递列表 - Flutter how can I pass a list in deep link (dynamic links) 颤振:列表<Map<String, dynamic> &gt; 不更新用户界面 - Flutter: List<Map<String, dynamic>> doesn't update ui 如何将第三个字符串输入到 List <map<string, string> &gt; 关于 Flutter? </map<string,> - How could I enter third string to List<Map<String, String>> on Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM