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

Time:02-22

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
}

CodePudding user response:

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.

CodePudding user response:

Try below code hope its help to you.

you can Used JsonEncoder-class here

Refer jsonEncode function here

import 'dart:convert';

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

  print(JsonEncoder().convert(map));
}

Your result:

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

CodePudding user response:

The right way of doing it is using Dart's inspect() function as shown here:

import 'dart:developer' as devtools show inspect;

const map = {
  'key1': 'Dog',
  'key2': 'Chicken',
};

void testIt() {
  devtools.inspect(map);
}
  • Related