toJsonForFirestore method

Map<String, dynamic> toJsonForFirestore()

Converts the JSON representation of this CloudSyncable object to a Firestore-compatible format, converting any valid date-time strings to Firestore Timestamps.

Implementation

Map<String, dynamic> toJsonForFirestore() {
  final json = toJson();

  json.forEach((key, value) {
    if (value is String) {
      // Check if the value is a string
      try {
        // Attempt to parse it as a DateTime
        final dateTime = DateTime.parse(value);
        json[key] = Timestamp.fromDate(dateTime); // Convert to Timestamp
      } catch (e) {
        // Not a valid DateTime string, leave it as is
      }
    }
  });

  return json;
}