stringifiedLog function

String? stringifiedLog(
  1. dynamic object
)

Converts the given object into its string representation.

The method attempts multiple strategies to stringify the provided object. If the object is a String, it is trimmed. If the object is a DiagnosticsNode, a deep string representation is returned. If the object provides a toJson method, the object is stringified using JSON serialization.

  • object: The object to be stringified.

Returns: The string representation of the object or null if the object is null.

Example usage:

final object = {"key": "value"};
final stringRepresentation = stringifiedLog(object);
print(stringRepresentation);  // Outputs: '{\n  "key": "value"\n}'

Implementation

String? stringifiedLog(dynamic object) {
  if (object == null) return null;
  if (object is String) return object.trim();
  if (object is DiagnosticsNode) return object.toStringDeep();

  try {
    object.toJson();

    dynamic toEncodable(dynamic object) {
      try {
        return object.toJson();
      } catch (_) {
        try {
          return '$object';
        } catch (_) {
          return describeIdentity(object);
        }
      }
    }

    return JsonEncoder.withIndent('  ', toEncodable).convert(object);
  } catch (_) {}

  try {
    return '$object'.trim();
  } catch (_) {
    return describeIdentity(object);
  }
}