pull method
Future<List<SyncRecord> >
pull({
- required String collection,
- DateTime? since,
- int? limit,
- int? offset,
- SyncFilter? filter,
override
Pull data from backend with optional pagination and filtering
Parameters:
collection: Collection name to pull fromsince: Only pull records modified after this timestamplimit: Maximum number of records to pulloffset: Number of records to skip (for pagination)filter: Optional sync filter for selective synchronization
Implementation
@override
Future<List<SyncRecord>> pull({
required String collection,
DateTime? since,
int? limit,
int? offset,
SyncFilter? filter,
}) async {
Query query = firestore.collection(collection);
// Use filter's since if provided, otherwise use since parameter
final effectiveSince = filter?.since ?? since;
if (effectiveSince != null) {
query = query.where('updatedAt',
isGreaterThan: Timestamp.fromDate(effectiveSince));
}
// Apply filter where conditions
if (filter?.where != null) {
for (final entry in filter!.where!.entries) {
query = query.where('data.${entry.key}', isEqualTo: entry.value);
}
}
// Apply pagination - use filter's limit if provided
final effectiveLimit = filter?.limit ?? limit;
if (effectiveLimit != null) {
query = query.limit(effectiveLimit);
}
// Note: Firestore doesn't have native offset, so we skip documents
// For better performance, consider using cursor-based pagination in production
if (offset != null && offset > 0) {
final skipSnapshot = await query.limit(offset).get();
if (skipSnapshot.docs.isNotEmpty) {
query = query.startAfterDocument(skipSnapshot.docs.last);
}
}
final snapshot = await query.get();
return snapshot.docs.map((doc) {
final data = doc.data() as Map<String, dynamic>;
var recordData = data['data'] as Map<String, dynamic>;
// Apply field filtering if specified
if (filter != null) {
recordData = filter.applyFieldFilter(recordData);
}
return SyncRecord(
recordId: doc.id,
data: recordData,
updatedAt: (data['updatedAt'] as Timestamp).toDate(),
version: data['version'] as int? ?? 1,
);
}).toList();
}