ALMAPA SA DE CV
ALMAPA SA DE CV

Reputation: 3

Making a query for firestore with multiple where clause

I am working on a flutter application which I m using firestore but now for searching, I have a query. The query will be like this

SELECT * 
FROM User 
WHERE age IS BETWEEN 16 TO 40 
  AND gender IS male 
  AND isSearching IS true

I have written the code for this query in flutter is like this

CollectionReference collection=FirebaseFirestore.instance.collection("Posts").doc(myMood).collection("Comments");

    Query  _searching=collection.where('isSearching',isEqualTo: true);
    Query _gender=_searching.where('gender',isEqualTo: preferredGender);
    Query _ageless=_gender.where('age',isEqualTo: values.start.round());
   Query _agemore=_ageless.where('age',isLessThanOrEqualTo: values.end.round() );

_agemore.get().then((querySnapshots) {
  print(_ageless.toString());
      if(querySnapshots.docs.length == 0)
      {
        print('data is empty');
      }
      else
      {
          print('data found');
      }

  });

I have manually set the data in firestore but I always give output data is empty. What will be the best way to write down a query for above statement

Upvotes: 0

Views: 73

Answers (2)

Could you try this

    CollectionReference collection=FirebaseFirestore.instance.collection("Posts").doc(myMood).collection("Comments");
    Query  _searching=collection.where('isSearching',isEqualTo: true);
    Query _gender=_searching.where('gender',isEqualTo: preferredGender);
    Query _agemore=_gender.where('age',isLessThanOrEqualTo: values.end.round() );
    
    _agemore.get().then((querySnapshots) {
    print(_ageless.toString());
    if(querySnapshots.docs.length == 0)
    {
        print('data is empty');
    }
    else
    {
        print('data found');
    
    }
    
});    
});

Upvotes: 0

Jo Eder
Jo Eder

Reputation: 338

You might have to create an extra key for this. Usually when you execute the query, android studio will throw an error and provide you with a link to create an index for you project.

Upvotes: 1

Related Questions