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.

Rkdio
  • 153
  • 2
  • 13

1 Answers1

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.

JMA
  • 803
  • 4
  • 9
  • 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. – Rkdio Jul 20 '21 at 20:22
  • So, do you think there is a way to somehow fix the plane part, which is the last stage of this for loop? – Rkdio Jul 20 '21 at 20:23
  • 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)``` – JMA Jul 21 '21 at 07:34
  • 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. – Rkdio Jul 21 '21 at 19:27
  • 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. – JMA Jul 22 '21 at 07:50
  • I know I asked a lot of questions, but for this if else condition, can you give an example adapted to my example? – Rkdio Jul 22 '21 at 22:01
  • I have to somehow do the array-contains-all operation. There has to be a way. – Rkdio Jul 22 '21 at 22:12
  • @Rkdio , pardon but do you wish to do this array-contains-all operation using firestore or using dart(flutter)? I think there might be a way to do this using dart (through a combination of the `where` filter and using the `intersect` method compared to what you're intersecting with), however Firestore's query's are rather limited, see [here](https://firebase.google.com/docs/firestore/query-data/queries#query_limitations), so I don't think that's currently possible within Firestore. – fabc Jul 29 '21 at 16:02