Home > Back-end >  Issue getting data with (too much) where clauses
Issue getting data with (too much) where clauses

Time:09-18

I'm brand new to Flutter and I've just encountered an error : I'm trying to retrieve all documents in a collection with multiples where clauses.

I'm working with Flutter v.3.3.1, Iphone 13 Pro simulator (iOS 15.5) for running my test app, Firebase Firestore as database.

For some context, here's how my data looks like :

Users {
  uid,
  name,
  mail,
  budget : {min, max}
  (sub-collection) passes : {Ad1, Ad6, ...},
  (sub-collection) likes : {Ad3, Ad5, ...}
}
Ads {
  uid,
  title,
  price,
  surface,
  ...
}

I want to get all the ads between a certain range of values (budget min/max) that the current user hasn't declined or liked.

var querySnapshot = await FirebaseFirestore.instance.collection('ads')
    .where('price', isGreaterThanOrEqualTo: userBudget.start)
    .where('price', isLessThanOrEqualTo: userBudget.end)
    .where('uid', whereNotIn: [...passedIds, ...matchesdIds]).get();

I've read Flutter Firestore doc about chaining where (using indexes, etc.) but couldn't figure how to do this. Index seems not to be a proper solution for my problem. I've tried things they recommended on the doc (https://firebase.google.com/docs/firestore/query-data/queries#compound_queries) but I still get this error :

An error occurred while parsing query arguments, this is most likely an error with this SDK. (
    0   CoreFoundation                      0x00000001803f3d70 __exceptionPreprocess   236
    1   libobjc.A.dylib                     0x000000018019814c objc_exception_throw   56
    2   FirebaseFirestore                   0x0000000103a1e994 _ZN8firebase9firestore4util16ObjcThrowHandlerENS1_13ExceptionTypeEPKcS4_iRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEE   540
    3   FirebaseFirestore                   0x0000000103a1ddc4 _ZN8firebase9firestore4util5ThrowENS1_13ExceptionTypeEPKcS4_iRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEE   64
    4   FirebaseFirestore                   0x0000000103d7e3a0 _ZN8firebase9firestore4util20ThrowInvalidArgumentIJNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEES9_EEEvPKcDpRKT_   72
    5   FirebaseFirestore                   0x0000000103d7da1c _ZNK8firebase9firestore3api5Query17ValidateNewFilterERKNS0_4core6FilterE   240
    6   FirebaseFirestore                   0x0000000103d7d160 _ZNK8firebase9firestore3api5Query6FilterERKN
Lost connection to device.

So, any help is welcomed :)

CodePudding user response:

From the documentation on Firestore's query limitations:

In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.

You're trying to have such conditions on two fields (price and uid), which isn't possible.

Also see:

  • Related