paginate method
Implementation
void paginate({
required PagedQueryExecutor<R> executor,
}) async {
/// showing loading with other contents intact.
emit(state.copyWith(status: PagedStatus.loading));
if (state.status == PagedStatus.end) emit(state);
/// This is inner function
Future<QuerySnapshot<R>> fetch(DocumentSnapshot<R>? skip) async {
final result = await executor(skip);
return result;
}
try {
if (state.status == PagedStatus.initial) {
final result = await fetch(null);
final items = result.docs.map((e) {
return FireData(object: e.data(), doc: e);
}).toList();
emit(
state.copyWith(
status: _hasReachedEnd(items.length),
items: items,
),
);
}
if (state.items.isNotEmpty) {
final last = state.items.last.doc;
final result = await fetch(last);
final items = result.docs.map((e) {
return FireData(object: e.data(), doc: e);
}).toList();
if (items.isEmpty) {
emit(state.copyWith(status: PagedStatus.end));
} else {
emit(state.copyWith(
status: _hasReachedEnd(items.length),
items: List.of(state.items)..addAll(items),
));
}
} else {
emit(state.copyWith(status: PagedStatus.end));
}
} catch (e) {
emit(state.copyWith(
status: PagedStatus.failure,
message: e.toString(),
));
}
}