encode static method

String encode(
  1. Object? object
)

Encode the provided object to a Json-formatted String.

Implementation

static String encode(Object? object) {
  // This is the only time [jsonEncode] should be used in the project.
  return jsonEncode(
    object,
    toEncodable: (nonEncodable) {
      //TODO: all the "dart native" types should be listed here
      if (nonEncodable is DateTime) {
        return nonEncodable.toUtc().toIso8601String();
      } else if (nonEncodable is ByteData) {
        return nonEncodable.base64encodedString();
      } else if (nonEncodable is Duration) {
        return nonEncodable.inMilliseconds;
      } else if (nonEncodable is Map && nonEncodable.keyType != String) {
        return nonEncodable.entries
            .map((e) => {'k': e.key, 'v': e.value})
            .toList();
      } else {
        return (nonEncodable as dynamic)?.toJson();
      }
    },
  );
}