next method

Future<FSQueryResult<T>> next()

Fetches the next page of objects.

Implementation

Future<FSQueryResult<T>> next() async {
  Deserializer? deserializer = FS.deserializers[T];
  if (deserializer == null) {
    throw UnsupportedError('No deserializer found for type: $T. Consider re-generating Firestorm data classes.');
  }

  //If there is a last document ID, start from after that
  if (lastDocumentID != null) {
    dynamic object = await FS.get.one<T>(lastDocumentID!, subcollection: subcollection);
    if (object != null) {
      if (object.id != null) {
        query = query.startAfter([object.id]);
      }
      else {
        throw ArgumentError('FSPaginator: The last document ID provided is not valid: $lastDocumentID');
      }
    }
  }

  //Limit the number of documents to fetch
  query = query.limit(numOfDocuments);

  //Run the query and return results:
  var documents = await query.get();
  List<T> objects = [];

  for (QueryDocumentSnapshot doc in documents.docs) {
    T object = deserializer(doc.data() as Map<String, dynamic>);
    objects.add(object);
  }
  if (objects.isEmpty) {
    return FSQueryResult(objects, null, null);
  }
  else {
    lastDocumentID = documents.docs[documents.docs.length - 1].id;
    return FSQueryResult(objects, documents.docs[documents.docs.length - 1], lastDocumentID);
  }
}