I am currently getting data related to Car details from firebase using FirebaseFirestore.instance.collection('cars').get().
I am saving all the Car instance in List<Cars> _cars = [];
using addCarsToList method which I am executing in didChangeDependencies{}
Future<void> addCarsToList({BuildContext context, FilterBy filter}) async {
var lastDocTracker =
Provider.of<LastDocumentTracker>(context, listen: false);
try {
var dcs = filter != null
? await carsCollection.orderBy('dueDate').limit(5).where('readyToRepair', isEqualTo: false).get()
: await carsCollection.orderBy('dueDate').limit(5).get();
//print(dcs.docs.last.data());
for (int i = 0; i < dcs.docs.length; i++) {
Cars carValue = singleCar(dcs, i);
if (_cars.every((item) => item.carId != carValue.carId)) {
_cars.add(carValue);
notifyListeners();
}
}
lastDocTracker.changeLastDocument(dcs.docs.last);
} catch (e) {
print(e);
}
}
here I am using lastDocumentTracer to track the last document to implement pagination.
Therefore in to achieve pagination I am using scroll controller and calling the getMore method
Future<void> getMore(
BuildContext context, QueryDocumentSnapshot lastSnap) async {
print('getMore called');
var lastDocTracker =
Provider.of<LastDocumentTracker>(context, listen: false);
var dcs = await carsCollection
.orderBy('dueDate')
.startAfterDocument(lastSnap)
.limit(5)
.get();
if (dcs.docs.isNotEmpty)
for (int i = 0; i < dcs.docs.length; i++) {
Cars carValue = singleCar(dcs, i);
if (_cars.every((item) => item.carId != carValue.carId)) {
_cars.add(carValue);
notifyListeners();
}
}
if (dcs.docs.isNotEmpty) lastDocTracker.changeLastDocument(dcs.docs.last);
return;
}
The problem that I am currently facing is I have three filters (All, Upcoming, Ready).
All means all cars from firebase without any filter.
Upcoming means the cars that are coming in future. (Saved as DateTime.millisecondsSinceEpoch in Firestore )
Ready means cars that are ready to repair (Saved as bool in firestore) Have a look into the screenshot
I am trying to filter using where((element) => isReadyToRepair) through the list of cars and use setState but unfortunately as I am limiting it to 5 items from Firebase and if there is no car with field isReadyToRepair = true then I can't scroll down and use scroll controller to call getMore and therefore it's showing empty and is not loading any data.
I need your help guys. Please help me. Please let me know if I haven't explained it well.