safeEncode method

String safeEncode(
  1. Object obj
)

encode method with toEncodable to correctly encode a DateTime object.

Implementation

String safeEncode(Object obj) {
  return json.encode(obj, toEncodable: (value) {
    if (value is DateTime) return value.toIso8601String();
    try {
      return value.toJson();
    } catch (e) {
      return null;
    }
  });
}