Search code examples
flutterflutter-dependenciesflutter-animationflutter-test

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


I have a Map in flutter

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

Actual Result

{
  key1: Dog,
  key2: Chicken
}

Expected Result

{
  "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.