0
String squery;

var sRef = _firestore
          .collection("vehicles1")
          .where("models", arrayContains: squery);

With this code i can display lists which include squery variable.

If I use this code in a for loop like this way;

List<String> item = ["motor", "car", "plane"];

var sRef;

for (int i = 0; i < item.length; i++) {
      sRef = _firestore
          .collection("vehicles1")
          .where("models", arrayContains: item[i]);
    }

I can display lists containing elements in the item list. But I am facing a problem here. When I want to use the for loop to display the lists containing the elements in the item list, the code additionally shows me the lists containing the last element in the item list.

For example, in this example, it shows me the lists that include motor, car, and plane, and it also shows lists that contain only plane, but I don't want to display only the lists that contain plane, I just want to display the lists that contain motor, car, plane.

So I just want to display All Doc list in my Firebase Documents.

And these are my Firebase Documents.

Motor Doc

Car Doc

Plane Doc

All Doc

When I run the code with for loop that I wrote above, I get an output like this, but I just want to get the list named ALL.

My Output

Why I get list named PLANE with list named ALL. I just want get to list named ALL.

1 Answer 1

0

In every iteration of the for loop you are overwriting the sRef variable, and checking that the models array contains one specific element of the item list. Therefore, in the last iteration, you are retrieving the arrays that contain the plane item, this is PLANE and ALL.

What you are trying is an array-contains-all like operation, which is not currently supported. You can check this other post for more information on this topic.

Sign up to request clarification or add additional context in comments.

8 Comments

This problem is for flutter firebase, I think it's a big shortcoming. While there are features like array contains and array contains any, there should be an absolute use of array contains all, by the way thank you for your answer.
So, do you think there is a way to somehow fix the plane part, which is the last stage of this for loop?
One option is to store the models data as a map structure instead of as an array, i.e. {motor: true, plane: true, car: true} so you will be able to filter like: _firestore.collection("vehicles1").whereField("models.plane", isEqualTo: true).whereField("models.car", isEqualTo: true).whereField("models.motor", isEqualTo: true)
Well, can I do this query in the for loop because I will query the elements in the list and their elements and numbers will be variable.
I think there is not a simple way to go through it. One option might be to iterate over all the documents in vehicles1, from each document retrieve the models array and check outside firestore if that array contains all the elements that you want (all the elements in the item array, in this case). If it does, retrieve that document, if not, continue with the next document.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.