pushUnsyncedItems method

  1. @override
Future<void> pushUnsyncedItems(
  1. List<T> items
)
override

Synchronize the given items with the remote server.

This method should handle both uploading new items and updating existing ones. It should also handle conflicts based on your chosen conflict resolution strategy.

Implementation

@override
Future<void> pushUnsyncedItems(List<T> items) async {
  try {
    final batch = firestore.batch();
    for (final item in items) {
      final docRef = (await _collectionWithoutConverter).doc(item.id);
      final data = item.toJsonForFirestore();

      // Set serverTimeSyncedAt and fileSyncedAt
      // using FieldValue.serverTimestamp()
      data['server_time_synced_at'] = FieldValue.serverTimestamp();

      final dataWithoutNull =
          Map.fromEntries(data.entries.where((e) => e.value != null));
      batch.set(
        docRef,
        dataWithoutNull as Map<String, Object?>,
        SetOptions(
          merge: true,
        ),
      ); // Use set with merge to avoid overwriting other fields
    }
    await batch.commit();
  } catch (e) {
    if (kDebugMode) {
      _logger.e('Error pushing unsynced items: $e');
    }
    rethrow;
  }
}