toPrettyJsonList function
String
toPrettyJsonList(
- dynamic input
)
Implementation
String toPrettyJsonList(dynamic input) {
List<dynamic>? jsonList;
if (input == null) {
return '[]';
}
if (input is String) {
if (input.isEmpty) {
return '[]';
}
// If input is a JSON string, decode it into a List
try {
jsonList = json.decode(input);
if (jsonList is! List) {
//return 'Invalid input type: expected JSON List string';
return '';
}
} catch (e) {
return '';
// return 'Error decoding JSON string: $e';
}
} else if (input is List) {
// If input is already a List, use it directly
jsonList = input;
} else {
return '';
}
// if (jsonList == null) {
// return '[]';
// }
try {
// Use JsonEncoder with 2-space indentation to make the JSON pretty
const jsonEncoder = JsonEncoder.withIndent(' ');
// Convert the List object into a pretty JSON string
return jsonEncoder.convert(jsonList);
} catch (e) {
// If there's an error during conversion, return an error message
// return 'Error converting to JSON: $e';
return '';
}
}