1

I am trying to perform a Query but i keep getting this error which i did not understand.

i am trying to perform a complex query

hasInequality == field': All where filters with an inequality (<, <=, >, or >=) must be on the same field. But you have inequality filters on 'FieldPath([ageFrom])' and 'FieldPath([toAge]).

here is my code:

    Future<List<DiscoverUserModel>> getListOfUserData() async {
        List<DiscoverUserModel> usersData = [];
        try {
          final value =
              await firestore.collection("users").doc(auth.currentUser.uid).get();
          final element1 = value.data();
          print('============> $element1');
          var ageFrom = element1['Type']['Age']['ageFrom'];
          var toAge = element1['Type']['Age']['toAge'];
          var distanceFrom = element1['Type']['Distance']['distranceFrom'];
          var distanceTo = element1['Type']['Distance']['distanceTo'];
          print('===========> $ageFrom');
          print('==========> $toAge');
          print('=========> $distanceFrom');
          print('========> $distanceTo');
          if (ageFrom == '18.0' && distanceFrom == '0.0') {
            await firestore.collection("users").get().then((value) {
              value.docs.forEach((element) {
                // print('resulting: ${element.data()['bio']}');
                DiscoverUserModel discoverUserModel = DiscoverUserModel(
                    '${element.data()['name']}',
                    '${element.data()['email']}',
                    '${element.data()['phoneNumber']}',
                    '${element.data()['profilePictureUrl']}',
                    id: element.data()['id'].toString(),
                    bio: element.data()['bio'].toString(),
                    gender: element.data()['gender'].toString());
                usersData.add(discoverUserModel);
                usersData
                    .removeWhere((element) => element.id == auth.currentUser.uid);
                return usersData;
              });
            });
          } else {

//this query is where i am getting the error ........... from here...
            await firestore
                .collection('users')
                .where('ageFrom', isGreaterThanOrEqualTo: ageFrom)
                .where('toAge', isLessThanOrEqualTo: toAge)
                .where('distanceTo', isLessThanOrEqualTo: distanceTo)
                .where('distranceFrom', isGreaterThanOrEqualTo: distanceFrom)
                .get()
                .then(
                  (value1) => value1.docs.forEach((element) {
                    print('========ele==>$element');
                    DiscoverUserModel discoverUserModel = DiscoverUserModel(
                      '${element.data()['name']}',
                      '${element.data()['email']}',
                      '${element.data()['phoneNumber']}',
                      '${element.data()['profilePictureUrl']}',
                      id: element.data()['id'].toString(),
                      bio: element.data()['bio'].toString(),
                      gender: element.data()['gender'].toString(),
                    );
                    usersData.add(discoverUserModel);
                    usersData.removeWhere(
                        (element) => element.id == auth.currentUser.uid);
                    return usersData;
                  }),
                );
          }
    
          return usersData;
        } catch (e) {
          print(e.toString());
          return null;
        }
      }

I don't want to create the index from the console myself because is not good way..

what i want to query

Gbenga B Ayannuga
  • 2,407
  • 3
  • 17
  • 38

1 Answers1

2

You need to call the query on a single document field.

Right now, you are querying 4 different document fields (ageFrom, toAge, distanceTo, distranceFrom)

This will not work. Firebase doesn't currently support querying multiple fields in a single request.

All the .where query statements need to be on a single field. Filter most on the server, do the rest on the client.

So filter one document field and then filter the documents you get from that query on the client-side.

Asjad Siddiqui
  • 562
  • 6
  • 8
  • okay... i will update the question with the picture of the console structure of what i want to Query, if you can help me out..... – Gbenga B Ayannuga Jun 11 '21 at 07:38
  • So filter one document field and then filter the documents you get from that query on the client-side...... i dont understand this.. can you put more light? – Gbenga B Ayannuga Jun 11 '21 at 07:51
  • Another problem I see is that you are using Strings as the value and you are using comparison operators ( >, <, ==, <=, >= ) on String values and not on integers or numbers. So you need to convert those strings to numbers (integers or doubles) first. – Asjad Siddiqui Jun 11 '21 at 08:27
  • By filtering on the client-side, I mean that you need to manually sort and filter the list of documents you get from the firebase query. So, you get the documents from firebase, and you loop through them to filter their contents according to your requirements. Let me know if you need any further clarification. – Asjad Siddiqui Jun 11 '21 at 08:32
  • Perhaps this will help: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Asjad Siddiqui Jun 11 '21 at 08:33
  • still the same, i have convert the string to number still yet, is showing that error, – Gbenga B Ayannuga Jun 11 '21 at 10:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233647/discussion-between-gbenga-b-ayannuga-and-asjad-siddiqui). – Gbenga B Ayannuga Jun 11 '21 at 10:44