Home > Mobile >  How to reverse Map values in dart
How to reverse Map values in dart

Time:12-20

I am trying to reverse Map values. But didin't get proper one,

here my Map

Map<String, dynamic> details = {
    "sms": {
      "very_low": false,
      "low": false,
      "medium": false,
      "high": false,
      "very_high": true
    }
  };

I want to get like this ;

{
  sms: {
     very_high: true,
     high: false,
     medium: false, 
     low: false, 
     very_low: false
   }
 }
               
            
   

If Anyone please share your ideas

CodePudding user response:

Let's say this is your "map":

Map<String, dynamic> details = {
  "sms": {
    "very_low": false,
    "low": false,
    "medium": false,
    "high": false,
    "very_high": true
  }
};

You can use this extension:

extension NewMap on Map {
  Map reverse() => Map.fromEntries(entries.toList().reversed);
}

and use it like this:

details["sms"] = (details["sms"] as Map<String, dynamic>).reverse();

print("details =$details"); //details = {sms: {very_high: true, high: false, medium: false, low: false, very_low: false}}

CodePudding user response:

   final details = 
     {"sms" : {"very_low" : false, "very_hight" : true, "very_medium" : false} };
   
   final outerMap = {};
  
  for(final m in details.entries){
    
    final reversedList = m.value.entries.toList().reversed;
    final innerMap = {};
    innerMap.addEntries(reversedList);
    outerMap.addAll({m.key : innerMap});
  }
  
  print(outerMap);
  //details = {sms: {very_medium: false, very_hight: true, very_low: false}}

CodePudding user response:

A solution that works for any Map no matter how much nested it is:

Object reverse(Object object) {
  if (object is Map) {
    return Map.fromEntries(object.entries.map((e) => MapEntry(e.key, reverse(e.value))).toList().reversed);
  }
  return object;
}

usage:

void main() async {
  Map<String, dynamic> details = {
    "sms": {
      "very_low": false,
      "low": false,
      "medium": false,
      "high": false,
      "very_high": true
    }
  };
  var newDetails = reverse(details);

  print(details);
  //{sms: {very_low: false, low: false, medium: false, high: false, very_high: true}}

  print(newDetails);
  //{sms: {very_high: true, high: false, medium: false, low: false, very_low: false}}
}


CodePudding user response:

import 'dart:convert';

Map<String, dynamic> yourMap = {
  "sms": {
    "very_low": false,
    "low": false,
    "medium": false,
    "high": false,
    "very_high": true
  }
};

Map<K, V> reverse<K, V>(Map<K, V> source) {
  return Map<K, V>.fromEntries(source.entries.toList().reversed);
}

main() {
  print(jsonEncode(yourMap));
  
  yourMap['sms'] = reverse(yourMap['sms']);
  
  print(jsonEncode(yourMap));
}

Prints:

{"sms":{"very_low":false,"low":false,"medium":false,"high":false,"very_high":true}}

{"sms":{"very_high":true,"high":false,"medium":false,"low":false,"very_low":false}}

Please note that this only works because Map behind the scenes constructs a LinkedHashMap that is insertion-ordered. If you construct other types of Map that are not, this may not work. Generally speaking, it might be wise to not rely on the order of items in a Map because it is not guaranteed unless you created the Map yourself.

Since this looks like it should be JSON: don't bother sorting this. It is supposed to be a machine-to-machine protocol, machines do not care if it looks neat. The less uneccessary code you write, the less bugs you produce.

  • Related