search in maps dart2 , same as list.indexOf?

3.3k Views Asked by At

I Use this sample for search in Map but not work :|:

  var xmenList = ['4','xmen','4xmen','test'];
  var xmenObj = {
  'first': '4',
  'second': 'xmen',
  'fifth': '4xmen',
   'author': 'test'
  };

  print(xmenList.indexOf('4xmen')); // 2
  print(xmenObj.indexOf('4xmen')); // ?

but I have error TypeError: xmenObj.indexOf$1 is not a function on last code line.

Pelease help me to search in map object simple way same as indexOf.

3

There are 3 best solutions below

4
On BEST ANSWER

I found the answer:

 print(xmenObj.values.toList().indexOf('4xmen')); // 2

or this:

  var ind = xmenObj.values.toList().indexOf('4xmen') ;
  print(xmenObj.keys.toList()[ind]); // fifth
1
On

Maps are not indexable by integers, so there is no operation corresponding to indexOf. If you see lists as specialized maps where the keys are always consecutive integers, then the corresponding operation should find the key for a given value. Maps are not built for that, so iterating through all the keys and values is the only way to get that result. I'd do that as:

K keyForValue<K, V>(Map<K, V> map, V value) {
  for (var entry in map.entries) {
    if (entry.value == value) return key;
  }
  return null;
}

The entries getter is introduced in Dart 2. If you don't have that, then using the map.values.toList().indexOf(value) to get the iteration position, and then map.keys.elementAt(thatIndex) to get the corresponding key.

If you really only want the numerical index, then you can skip that last step. It's not amazingly efficient (you allocate a new list and copy all the values). Another approach is:

int indexOfValue<V>(Map<Object, V> map, V value) {
  int i = 0;
  for (var mapValue in map.values) {
    if (mapValue == value) return i;
    i++;
  }
  return -1;
}
1
On

You can search using .where(...) if you want to find all that match or firstWhere if you assume there can only be one or you only want the first

var found = xmenObj.keys.firstWhere(
    (k) => xmenObj[k] == '4xmen', orElse: () => null);
print(xmenObj[found]);