prettyFormatBody function
Pretty-prints data as indented JSON for display in the DevTools
panel — the common case for an API request/response body. Falls
back to a short, readable summary for the two shapes JSON can't
represent meaningfully: raw bytes (Uint8List, e.g. a
ResponseType.bytes download) and Dio's multipart FormData
request bodies. Anything else that isn't valid JSON (plain text,
HTML, XML, ...) is shown as-is via Object.toString.
Implementation
String prettyFormatBody(Object? data) {
if (data == null) return 'null';
if (data is Uint8List) return _formatBytes(data);
if (data is FormData) return _formatFormData(data);
try {
final value = data is String ? jsonDecode(data) : data;
return const JsonEncoder.withIndent(' ').convert(value);
} catch (_) {
return data.toString();
}
}