getDocuments<T> method
Future<List<NimbostratusDocumentSnapshot<T?> > >
getDocuments<T>(
- Query<
T> docQuery, { - GetFetchPolicy fetchPolicy = GetFetchPolicy.serverOnly,
- NimbostratusFromFirestore<
T> ? fromFirestore,
Executes a Firestore Query
for documents against the in-memory cache or server according to the specified GetFetchPolicy.
Implementation
Future<List<NimbostratusDocumentSnapshot<T?>>> getDocuments<T>(
Query<T> docQuery, {
GetFetchPolicy fetchPolicy = GetFetchPolicy.serverOnly,
NimbostratusFromFirestore<T>? fromFirestore,
}) async {
QuerySnapshot<T>? snap;
switch (fetchPolicy) {
case GetFetchPolicy.cacheFirst:
case GetFetchPolicy.cacheOnly:
final snap = await docQuery.get(const GetOptions(source: Source.cache));
final docs = snap.docs;
// If there is no data in the cache to satisfy the get() call, QuerySnapshot.get() will return an empty
/// QuerySnapshot with no documents. If this is a cache-first operation, we then go check the server
/// to see if we can satisfy the query there.
if (docs.isNotEmpty) {
return docs.map<NimbostratusDocumentSnapshot<T?>>((doc) {
final docBloc = _documents[doc.reference.path];
// If the document is already in the NS cache, return the NS value. Otherwise
// cache it to NS and fallback to the Firestore cache value.
if (docBloc != null) {
return docBloc.value as NimbostratusDocumentSnapshot<T?>;
}
return _updateFromSnap(doc);
}).toList();
} else if (fetchPolicy == GetFetchPolicy.cacheFirst) {
return getDocuments(
docQuery,
fetchPolicy: GetFetchPolicy.serverOnly,
);
}
return [];
case GetFetchPolicy.serverOnly:
try {
snap = await docQuery.get();
return snap.docs
.map(
(snap) => _updateFromSnap(snap, fromFirestore: fromFirestore),
)
.toList();
} catch (e) {
return [];
}
}
}