forceObjToString function

String forceObjToString(
  1. Object? obj
)

Converts an object to a string, returning a fallback representation if the object's toString() method throws an exception.

This is a defensive utility, primarily for logging and debugging, to prevent a misbehaving object from crashing the application. In debug mode, it triggers an assertion to notify the developer of the issue.

Implementation

String forceObjToString(Object? obj) {
  try {
    return obj.toString();
  } catch (e) {
    return '${obj.runtimeType}@${obj.hashCode.toRadixString(16)}';
  }
}