0

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

2 Answers 2

1

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.

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

1 Comment

I have created an index for it
0

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');
    
    }
    
});    
});

Comments

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.