safeEncode method
Encodes a Dart object into a JSON string safely.
Implementation
String safeEncode() {
try {
return jsonEncode(this);
} catch (e) {
print('Failed to encode JSON: $e');
return '{}'; // Return an empty JSON object as a safe fallback.
}
}