Roger
Roger

Reputation: 161

How to convert a Map<String,String> to a human readable String (in Dart)?

I'm trying to convert a Map to a human readable string.

So let's say I have this Map:

Map<String, String> map = { "foo": "lorem", "bar": "ipsum" };

I want to convert it too the following string, including the indents:

{
    "foo": "lorem",
    "bar": "ipsum"
}

Upvotes: 5

Views: 9480

Answers (2)

Roger
Roger

Reputation: 161

use: new JsonEncoder.withIndent(" ").convert(map)

From the new dartpad linq with the same url fragment 685e0fc43fb4e70c602e

import "dart:convert";

main() {
  var map = { "foo": "lorem", "bar": "ipsum" };
  print(new JsonEncoder.withIndent("    ").convert(map));
}

see at dartpad: https://dartpad.dartlang.org/685e0fc43fb4e70c602e (old link is dead)

Upvotes: 11

Santiago Suarez
Santiago Suarez

Reputation: 1

Using this code you can convert your map to a JSON string.

import "dart:convert";

var mapInJsonString = json.encode(map);

Upvotes: 0

Related Questions