How to retrieve store data from startIndex to endIndex? and everytime pressing the paging, it will load another one
@override
Future<bool> handlePageChange(int oldPageIndex, int newPageIndex) async {
await Future.delayed(const Duration(seconds: 3));
int startIndex = newPageIndex * rowsPerPage;
int endIndex = startIndex + rowsPerPage;
if (startIndex < pageCount) {
if (endIndex > storesLength) {
endIndex = storesLength as int;
}
paginatedDataSource = await DBService.getStoresData();
// paginatedDataSource =
// storeList.getRange(startIndex, endIndex).toList(growable: false);
buildDataGridRows();
} else {
paginatedDataSource = [];
}
notifyListeners();
return true;
}
/////////////////////////database////////////////////////
static Future<List<StoresModel>> getStoresData() async {
final List<StoresModel> storesList = [];
try {
final userController = Get.find<UserController>();
final companyId = userController.companyId;
QuerySnapshot query = await FirebaseFirestore.instance
.collection(kCompanyCollection)
.doc(companyId)
.collection(kStoresCollection)
.limit(7)
.get();
if (query.docs.isNotEmpty) {
for (QueryDocumentSnapshot document in query.docs) {
final data = document.data() as Map<String, dynamic>;
final storesListModel = StoresModel.fromMap(document.id, data);
storesList.add(storesListModel);
}
}
} catch (e) {
print("Error retrieving data: $e");
}
return storesList;
}
Hi WONG PEI SAN,
Based on the information provided and the Firestore database structure, to
fetch data from Firestore, begin by retrieving the current document. Then, use
the `startAfterDocument` method to obtain the next set of documents.
This process depends on the values of `newPageIndex` and `rowsPerPage`.
You can utilize a unique property (such as the document ID or index) to access
the desired document.
For your convenience, we've included a sample below. In this example, `rowsPerPage` is set to 3, and we calculate the `startIndex` by multiplying it with `newPageIndex`. Subsequently, we obtain the `lastDocument` by matching the `startIndex` with the `companyID` value in my Firebase database. Using the `lastDocument`, you can retrieve another set of rows from Firestore using the `startAfterDocument` method.
For a more comprehensive understanding of this process,
please consult the provided code snippet and sample.
|
class CompanyDataSource extends DataGridSource {
… final collection = FirebaseFirestore.instance.collection('companies');
Stream<QuerySnapshot> getDataFromFirestore() { return collection.limit(3).snapshots(); }
… @override Future<bool> handlePageChange(int oldPageIndex, int newPageIndex) async { // Here we are getting the start index of the page int startIndex = newPageIndex * rowsPerPage;
List<Company> paginatedData = [];
// Here we are checking whether the page is initial loading or not // because we get the first page data from the `getDataFromFirestore` method at the initial loading // and we don't need to call the `getDataFromFirestore` method again if (!isInitialLoading) { // Here we are getting the last document from the FireStore collection // based on the start index // In my firestore collection, I have added the companyID from 1 to 15 // So, I am getting the last document based on the start index var lastDoument = await collection .where("companyID", isEqualTo: startIndex == 0 ? '1' : startIndex.toString()) .get() .then((value) => value.docs.first);
QuerySnapshot<Map<String, dynamic>> snap;
// Here we are checking whether the start index is 0 or not // because if the start index is 0, then we need to get the first 3 documents if (startIndex == 0) { snap = await collection.startAtDocument(lastDoument).limit(3).get(); } else { // Here we are getting the next 3 documents from the FireStore collection snap = await collection.startAfterDocument(lastDoument).limit(3).get(); }
// Here we are mapping the data to the DataGridRow // and adding it to the dataGridRows list. // The dataGridRows is the local collection // which is assigned to the `rows` property of DataGridSource. for (var data in snap.docs) { paginatedData.add(Company( id: data['companyID'], companyName: data['companyName'], city: data['city'], country: data['country'])); } // To build the DataGridRow from the paginated data buildDataGridRow(paginatedData); } else { paginatedData = []; } notifyListeners();
if (isInitialLoading) { isInitialLoading = false; } return true; } … } |
Regards,
Tamilarasan
Attachment: sampleandoutput_88db88f7.zip
- 1 Reply
- 2 Participants
-
WP WONG PEI SAN A20EC0170
- Oct 26, 2023 07:48 AM UTC
- Oct 31, 2023 03:50 PM UTC