convertObjectToString static method
Converts an object to a formatted string representation
@param data The data to convert @param isFormatText Whether to format the text (default: true) @return The string representation of the data
Implementation
static String convertObjectToString(dynamic data,
{bool isFormatText = true, int? maxLength}) {
String result = '';
Map<String, dynamic>? map;
if (data is FormData) {
map ??= {};
map = data.fields.fold(<String, dynamic>{}, (previousValue, element) {
previousValue[element.key] = element.value;
return previousValue;
});
} else if (data is Map<String, dynamic>) {
map = data;
} else if (data is List) {
if (isFormatText) {
result = convertJsonToString(data);
} else {
result = jsonEncode(data);
}
} else if (data is String) {
final json = convertStringToJson(data);
if (json == null) {
result = data.toString();
} else {
result = convertJsonToString(json);
}
} else {
result = data.toString();
}
if (isFormatText && map != null) {
result = convertJsonToString(map);
}
if (maxLength != null && result.length > maxLength) {
result = '${result.substring(0, maxLength)}\n...';
}
return result;
}