如何过滤 List<Map<String, dynamic>> 以在 Flutter 中获取值?

How can I filter List<Map<String, dynamic>> to get a value in Flutter?

我有一个 json 数据,我正在使用 await jsonDecode. 加载到 List<Map<String, dynamic>>

例如,我有一个;

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

我需要过滤儿子以获得成熟度值:689.19

此外,如果我有一个数量 = 12500 和价值 = 689.19,我需要获得 24 口径。

如何筛选列表> 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"}
]

更新: 在@Muldec 的帮助下,我终于明白了。

首先我将上面的子加载到列表中,如下所示。并应用@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']);

您可以使用列表中的 firstWherewhere 方法来获取您需要的 Map 元素或 Map 的列表,具体取决于您的搜索条件。

假设您完全确定您的搜索条件只会给您一个项目(或者您只关心第一个符合您条件的项目)这里有一个代码示例 firstWhere:

List<Map<String, dynamic>> myList = ....;

final item = myList.firstWhere((e) => e['amount'] == '12500' && e['caliber'] == '24');
print(item['maturity']);

根据数量和成熟度找到口径,只是在firstWhere方法

中更改测试而已