Flutter Firebase whereIN 10 limit Problem

0

Issue

I have id list like this;

List<dynamic> saveIDList = []; The values ​​in this list change according to the id saved by the user.

I try to show the posts saved by the user. So I create a query like this;

 Query saveIDRef = _firestore
              .collectionGroup("Posts")
              .where("postID", whereIn: saveIDList);

After creating this query, I call the saveIDRef in the stream builder. So far, there is no problem in my code, it works properly, but when the length of the saveIDlist exceeds 10, I get a warning.

It is not possible for me to change my database form or the way I query my saveIDList, how can I solve this problem?

I tried different solutions but it didn’t work.

For Example I create chunk list like this;

var chunks = [];

for (var i = 0; i < saveIDList.length; i += 10) {
      chunks.add(saveIDList.sublist(
          i, i + 10 > saveIDList.length ? saveIDList.length : i + 10));
    }

and I seperated values in saveIDList then I used chunks in whereIN query like this;

 Query saveIDRef = _firestore
                  .collectionGroup("Posts")
                  .where("postID", whereIn: chunks);

But that didn’t work either. When I query chunks list like this;

Query saveIDRef = _firestore
                      .collectionGroup("Posts")
                      .where("postID", whereIn: chunks[0]);

it works. But I want to Query all elements in chunks list not the only first element of chunks list. When I put this code in to the for loop, I get error of whereIN query cannot use more than once.

Solution

As stated in the Firebase documentation regarding the simple and compound queries in Cloud Firestore, there are some limitations on the logical queries:

Cloud Firestore provides limited support for logical OR queries. The in, and array-contains-any operators support a logical OR of up to 10 equality (==) or array-contains conditions on a single field. For other cases, create a separate query for each OR condition and merge the query results in your app.

Thus a possible solution would be to create chunks of your list and query those chunks separately as you have tried. For instance you can use the List subList method of the List object to create those sublists or chunks.

Answered By – llompalles

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More