0

When I get an object directly from a collection in firestore it works just fine, although when I query for it using the where command nothing appears. I'm wonder if I'm incorrectly using the API or there is a bug

 await FirebaseFirestore.instance
        .collection('games')
        .where('status', arrayContains: 'GameStatus.uninitialized')
        .get()
        .then((value) => {
              if (value.docs.isNotEmpty)
                {
                  print(value.docs[0].reference.id),
                }
              else
                {
                  print('nada'),
                }
          
            });

enter image description here

Here is the firestore: firestore

Gene
  • 374
  • 3
  • 8

3 Answers3

0

arrayContains parameter takes string not list it should look like this

 await FirebaseFirestore.instance
            .collection('games')
            .where('status', arrayContains: 'GameStatus.uninitialized')
            .get()
            .then((value) => {
                  if (value.docs.isNotEmpty)
                    {
                      print(value.docs[0].reference.id),
                    }
                  else
                    {
                      print('nada'),
                    }
              
                });
  • Hey thanks for the answer! Using a list or an object made no difference! I settled for just using isEquals for now, although this still really bugs me. Let me update the original post to clear up any confusion – Gene Aug 12 '21 at 20:20
  • can you post the firestore document screenshot i want to see what kind of data you are trying to retrieve its hard to see on the jif – Sintayew Zebene Aug 12 '21 at 20:45
  • Just added it to the original post – Gene Aug 17 '21 at 19:13
0

You cannot query firestore like that, I will refer you to this question

Instead, try to put status in the root of document and then query it like this

 await FirebaseFirestore.instance
        .collection('games')
        .where('status', isEqualTo: 'GameStatus.uninitialized')
        .get()
        .then((value) => {
              if (value.docs.isNotEmpty)
                {
                  print(value.docs[0].reference.id),
                }
              else
                {
                  print('nada'),
                }
          
            });
Sajad Abdollahi
  • 1,593
  • 1
  • 7
  • 19
0

status field is not a type of array you can only use arrayContains for arrays instead you should use likethis

 await FirebaseFirestore.instance
                .collection('games')
                .where('status', isEqualTo: 'GameStatus.uninitialized')
                .get()
                .then((value) => {
                      if (value.docs.isNotEmpty)
                        {
                          print(value.docs[0].reference.id),
                        }
                      else
                        {
                          print('nada'),
                        }
                  
                    });

or if you trying to query players field You currently can't build a query that looks for an object field within an array but you can use for string or integer like this

await FirebaseFirestore.instance
                .collection('games')
                .where('players', arrayContains: 'GameStatus.uninitialized')
                .get()
                .then((value) => {
                      if (value.docs.isNotEmpty)
                        {
                          print(value.docs[0].reference.id),
                        }
                      else
                        {
                          print('nada'),
                        }
                  
                    });
  • Status field is a string, which is an array of characters, no? The documentation instructed this approach of querying strings in a document. – Gene Aug 24 '21 at 04:46
  • no unfortunately the are not the same! if that's your goal change Status field data type into an array and assign the characters for each indexes – Sintayew Zebene Aug 26 '21 at 11:03
  • From what I understand, I can only search top level, not nested arrays, and I'm looking just to get the field at the top (like status, or id) with a partial piece of the name – Gene Aug 27 '21 at 12:08