Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firestore startAt() endtAt() not working when orderby string values

I Am trying to use startAt([1]) and endAt([3]) when order by title but it doesn't work.

QuerySnapshot snapshot = await Firestore.instance.collection('items')
  .orderBy("title", descending: false).startAt([1]).endAt([2]).limit(2).getDocuments();

When I try by order by rank startAt([1]) and endAt([3]) it works.

QuerySnapshot snapshot5 = await Firestore.instance.collection('items')
  .orderBy("rank", descending: false).startAt([1]).endAt([2]).limit(2).getDocuments();

Works when i don't use endAt()

Why doesn't it work when i order by title?

QuerySnapshot snapshot = await Firestore.instance.collection('items')
  .orderBy("title", descending: false).startAt([1]).limit(2).getDocuments();

Anyone have any idea?

What i have tried: 1. ensure packages are up to date 2. created indexes on firestore

[THIS DOES NOT WORK] QuerySnapshot snapshot = await Firestore.instance.collection('items').orderBy("title", descending: false).startAt([1]).endAt([2]).limit(2).getDocuments();

[THIS WORKS]
QuerySnapshot snapshot5 = await Firestore.instance.collection('items')
  .orderBy("rank", descending: false).startAt([1]).endAt([2]).limit(2).getDocuments();

[THIS WORKS]
QuerySnapshot snapshot = await Firestore.instance.collection('items')
  .orderBy("title", descending: false).startAt([1]).limit(2).getDocuments();

What i expected was to be able to use endAt() when filtering but its throwing error

like image 523
Mozes Ong Avatar asked Oct 28 '25 03:10

Mozes Ong


1 Answers

I'm going to assume that your rank fields are numeric, and your title field is a string. In that case this is expected behavior.

Firestore queries filter by value and not by offset. So when you:

.orderBy("rank", descending: false).startAt(1).endAt(2)

The documents are ordered by their rank, and then documents with a rank between 1 and 2 (inclusive) are returned.

When you do:

.orderBy("title", descending: false).startAt(1).endAt(2)

The documents are ordered by their title, and then documents with a title between 1 and 2 are returned. But since the titles are strings and not numbers, this means that no document matches the condition and nothing is returned.

Firestore does not have the concept of offset queries, where you tell it to skip the first n results.

like image 86
Frank van Puffelen Avatar answered Oct 30 '25 13:10

Frank van Puffelen