pushChange method
Future<void>
pushChange({
- required String collection,
- required Map<
String, dynamic> document, - required SyncOperation operation,
override
Pushes a single document change to the remote backend.
operation determines whether the backend performs an insert, update/
merge, or delete.
Implementation
@override
Future<void> pushChange({
required String collection,
required Map<String, dynamic> document,
required SyncOperation operation,
}) async {
final id = document['id'] as String?;
if (id == null) {
throw ArgumentError(
'FirestoreSyncSource.pushChange: document must contain an "id" field.',
);
}
// Extract userId from the document if present for user-scoped collections
final userId = document['userId'] as String?;
final ref = _collectionRef(collection, userId: userId).doc(id);
switch (operation) {
case SyncOperation.create:
case SyncOperation.update:
final payload = _preparePayload(document);
await ref.set(payload, SetOptions(merge: true));
case SyncOperation.delete:
// Honour soft-delete flag if present; hard-delete otherwise
final isDeleted = document['isDeleted'] as bool? ?? false;
if (isDeleted) {
// Write the soft-delete marker so other clients can sync the deletion
await ref.set({
'isDeleted': true,
timestampField: FieldValue.serverTimestamp(),
}, SetOptions(merge: true));
} else {
await ref.delete();
}
}
}