Firestore null query returns non-nullable field

266

Solution 1

FlutterFire member here:

Doing isEqualTo: null is not supported. It is behaving as if you did where('key'), aka doing nothing.

As 呂學洲 mentioned, it is required to use isNull: true

Solution 2

You should use isNull instead.

query.where(key, isNull: true); //bar
query.where(key, isNull: false); //baz

I can't find any document describing why things implemented this way. But I'm guessing that sending isEqualTo/isNotEqualTo with null is telling Firestore to get documents that exist the field.

You can try querying document bak which doesn't have field key.

Share:
266
iDecode
Author by

iDecode

Updated on December 31, 2022

Comments

  • iDecode
    iDecode over 1 year

    enter image description here

    enter image description here

    var collection = FirebaseFirestore.instance.collection('foo');
    var query1 = collection.where('key', isEqualTo: null);
    var query2 = collection.where('key', isNotEqualTo: null); 
    

    Both query1 and query2 returns bar and baz documents. But according to my understanding, query1 should have returned bar only.

    • Dharmaraj
      Dharmaraj almost 3 years
      I wasn't able to replicate this issue. This could be something with Flutter SDK then. Javascript SDK worked fine for me.
  • iDecode
    iDecode over 2 years
    But if you remove key from bar, then also it returns the same results.
  • iDecode
    iDecode over 2 years
    I knew isNull part, and it works as intended but the question was why isEqualTo: null also returning "value" documents. Even if there's no key, both isEqualTo and isNotEqualTo returns same documents. If no answer is provided to my exact question, then I'll accept yours (although I already knew this thing)
  • Diwyansh
    Diwyansh over 2 years
    can you add that to your question as well?
  • iDecode
    iDecode over 2 years
    I'm sure you must have tested your code before writing the answer, so in your sample you can remove key and see it returning the same results.
  • iDecode
    iDecode over 2 years
    Thanks, but can you point me to the docs where it says "isEqualTo: null" is not supported, I must have missed it.
  • Rémi Rousselet
    Rémi Rousselet over 2 years
    I doubt there's docs about that. It's about how Dart and where works. Since Dart has no undefined, doing isEqualTo:null is identical to not passing it
  • iDecode
    iDecode over 2 years
    Ok, that made sense. I'm accepting your answer but will award the bounty to another user as he mentioned the working solution before you ;)