flutter 如何在嵌套Map中搜索(过滤)并返回整个Map元素

kpbpu008  于 2022-11-25  发布在  Flutter
关注(0)|答案(2)|浏览(230)

我试图过滤Map,作为回报,我想要每一个过滤的Map元素。

代码:-

Map<String, Map<int, int>> temp = {Basic Terms: {1: 0}, Table and Column Naming Rules: {1: 1}};
var temp = temp.keys.where(element) => element.contains("basic"));
print(temp);

输出:-

I/flutter (30857): (Basic Terms)

我要的输出:-

I/flutter (30857): {Basic Terms: {1: 0}}
sdnqo3pr

sdnqo3pr1#

您需要迭代entries而不是keys,然后将List<MapEntry>转换回Map

Map<String, Map<int, int>> temp = {
  'Basic Terms': {1: 0}, 
  'Table and Column Naming Rules': {1: 1}
};

var temp2 = Map.fromEntries(
  temp.entries.where(
    (entry) => entry.key.contains('Basic Terms')
  )
);
print(temp2);

输出:

{Basic Terms: {1: 0}}
relj7zay

relj7zay2#

Map<String, Map<int, int>> temp = {'Basic Terms': {1: 0}, 'Table and Column Naming Rules': {1: 1}};
  var thisTemp = temp.entries.firstWhere((element) => element.key.contains("Basic"));
  print(thisTemp.toString());
}

相关问题