SPIDEY
SPIDEY

Reputation: 23

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

Upvotes: 1

Views: 99

Answers (2)

jbryanh
jbryanh

Reputation: 2033

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

Upvotes: 0

Ben Konyi
Ben Konyi

Reputation: 3229

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

Upvotes: 3

Related Questions