getJson function
A Dart function that takes a JSON-serializable object and returns a formatted JSON string. The function also logs the formatted JSON string to the console with an optional label.
Parameters:
- jsonObject: the JSON-serializable object to be converted to a formatted JSON string.
- name: an optional string used as a label for the logged output.
Returns: A formatted JSON string representing the input object.
Example usage:
var myObject = {"foo": "bar"};
getJson(myObject, name: "My Object"); // logs formatted JSON string with label "My Object"
Implementation
String getJson(jsonObject, {name}) {
var encoder = const JsonEncoder.withIndent(" ");
log(encoder.convert(jsonObject), name: name ?? "");
return encoder.convert(jsonObject);
}