androidflutterdartnested-map

How to search(filter) in nested map and return entire map elements


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}}

Solution

  • 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}}