Sunday, 15 May 2022

Firestore doesn't retrieve more than 600 items

I am trying to load some data from Firestore in a Flutter app. When I load upto 600 items , it works fine. but even if I try retrieving 700I don't get anything.

Here is the code:

Stream<List<Booking>> listenToWeekEntries()  {
    return FirebaseFirestore.instance
        .collection('bookings')
        .orderBy('entryTimestamp')
        .where('entryTimestamp',isGreaterThanOrEqualTo: getWeekFilter()[0]/1000, isLessThanOrEqualTo: getWeekFilter()[1]/1000)
        .limitToLast(1000).snapshots().map((event) => List.from(event.docs.map((e) => Booking.fromSnapshot(e))));
          //

  }

I only have 1 collection 'bookings' and in it I have about 50000 documents(bookings) (growing at about 120/day). Each booking has about 13 fields. I am trying to create charts representing the counts on different days, months and years. What's the best way to do this?

I have tried looking around for a solution but have had not luck.

I have also tried the following :

late DocumentSnapshot _lastDocument;
List<Booking> entries = List.empty(growable: true);

Constructor(){
    getEntries();
}

void getEntries() async {
    var q = await FirebaseFirestore.instance
        .collection('bookings')
        .orderBy('entryTimestamp')
        .where('entryTimestamp',isGreaterThanOrEqualTo: getWeekFilter()[0]/1000, isLessThanOrEqualTo: getWeekFilter()[1]/1000)
        .limitToLast(50).get();
    entries.addAll(q.docs.map((e) => Booking.fromSnapshot(e)));
    notifyListeners();
    print('${entries.length}');
    if(entries.length < 2000){
      _lastDocument = q.docs[q.docs.length - 1];
      getMoreEntries();
    }
  }

void getMoreEntries() async {
    print('WEEK_DEBUG | Getting more entries after $_lastDocument.');
    var q = await FirebaseFirestore.instance
        .collection('bookings')
        .orderBy('entryTimestamp')
        .where('entryTimestamp',isGreaterThanOrEqualTo: getWeekFilter()[0]/1000, isLessThanOrEqualTo: getWeekFilter()[1]/1000)
        .startAfterDocument(_lastDocument)
        .limitToLast(50).get();

    entries.addAll(q.docs.map((e) => Booking.fromSnapshot(e)));
    notifyListeners();
    _lastDocument = q.docs[q.docs.length - 1];
    if(entries.length < 2000){
      getMoreEntries();
    }
  }

 List getWeekFilter(){
    var now = DateTime.now();
    var firstDay = now.subtract(const Duration(days: 7));
    var lastDay = now.add(const Duration(days: 7));
    var from = DateTime(firstDay.year, firstDay.month, firstDay.day).millisecondsSinceEpoch;
    var to = DateTime(lastDay.year, lastDay.month, lastDay.day).millisecondsSinceEpoch;
    return [from, to];

  }

With this, getMoreEntries() does get called however the total length of entries remains 50.

Any help is greatly appreciated.



from Firestore doesn't retrieve more than 600 items

No comments:

Post a Comment