Home > Software design >  How to search(filter) in nested map and return entire map elements
How to search(filter) in nested map and return entire map elements

Time:11-22

I am trying to filter the map and in return, I want every filtered map element.

Code:-

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);

Output:-

I/flutter (30857): (Basic Terms)

Output I want :-

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

CodePudding user response:

You want to iterate over entries not keys, and then convert the List<MapEntry> back to a 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);

Which outputs:

{Basic Terms: {1: 0}}

CodePudding user response:

  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());
}
  • Related