sanitizeCloudDataToSend function
Returns a sanitized version of the given data
to be sent to the cloud
storage.
Updates the SDKConstants.createdAt
and SDKConstants.updatedAt
fields
with the current time.
Implementation
Map<String, dynamic> sanitizeCloudDataToSend(
Map<String, dynamic> data, {
required String? docId,
bool hidePrivateFields = false,
}) {
if (data.isEmpty) return data;
// Breaks reference and allows to modify the data.
data = {...data};
// Remove private fields.
if (hidePrivateFields) {
for (final field in privateDocumentFields) {
data.remove(field);
}
} else {
// Late because it can potentially be unused;
late final DateTime now = DateTime.now();
data[SDKConstants.createdAt] =
serializedCosmicValue(data[SDKConstants.createdAt] ?? now);
data[SDKConstants.updatedAt] = serializedCosmicValue(now);
if (docId != null) {
data[SDKConstants.id] = docId;
}
// Sort private fields to the bottom.
for (final field in privateDocumentFields) {
if (data.containsKey(field)) {
final value = data.remove(field);
data[field] = value;
}
}
}
// Remove any key that is empty. Empty keys are not allowed on Firestore.
data.remove('');
return data;
}