How can I made a JSON string out of a collection in dart, as I can do it with Maps. The docs say I can pass a map or a an array into the JSON.stringify() method. But there are no Array data type in Dart and passing a collection gives me an exception.
I’ve a naive workaround, but I wonder if there will be a better way to do this:
String s = '[';
bool first=true;
_set.forEach(function(item){
if (first) {
first = false;
} else {
s+=',';
}
s += JSON.stringify(item);
});
s +=']';
print(s);
return s;
Passing a list works for me:
dart-sdk/lib/frog/server/dart_json.dartjson:dartusing this code:
prints this JSON snippet:
Doesn’t work for new
Set.from(...)which is expected, given that JSON only deals in maps and lists.