When I print a Map in Flutter, double quotes are missing in the Log

I have a Map in flutter

Map<String, dynamic> map = {
  'key1': 'Dog',
  'key2': 'Chicken',
};

I need to print the map like this,

   {
      "key1": "Dog",
      "key2": "Chicken"
    }

But I get the log prints the Map like this("double quotes are missing"),

{
  key1: Dog,
  key2: Chicken
}

>Solution :

You can use any of the following approaches.

Map<String, dynamic> map = {
  'key1': 'Dog',
  'key2': 'Chicken',
};
  
print(json.encode(map)); //approach - 1
print(JsonEncoder.withIndent('  ').convert(map)); //approach - 2

Note: don’t forget to import dart:convert.

Leave a Reply