Dart json.encode returns json string with key values without quotes

This is working as expected

import 'dart:convert';

void main() {
  var resBody = {};
  resBody["email"] = "[email protected]";
  resBody["password"] = "admin123";
  var user = {};
  user["user"] = resBody;
  String str = json.encode(user);
  print(str);
}

prints

{"user":{"email":"[email protected]","password":"admin123"}}

DartPad example

[or]

import 'dart:convert';

void main() {
  const JsonEncoder encoder = JsonEncoder.withIndent('  ');
  try {
  var resBody = {};
  resBody["email"] = "[email protected]";
  resBody["password"] = "admin123";
  var user = {};
  user["user"] = resBody;
  String str = encoder.convert(user);
  print(str);
  } catch(e) {
    print(e);
  }
}

which gives you the beautified output

{
  "user": {
    "email": "[email protected]",
    "password": "admin123"
  }
}

Tags:

Dart

Flutter