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

Issue

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.

Answered By – nagendra nag

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *