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
.
Author by
iDecode
Updated on December 31, 2022Comments
-
iDecode over 1 year
var collection = FirebaseFirestore.instance.collection('foo'); var query1 = collection.where('key', isEqualTo: null); var query2 = collection.where('key', isNotEqualTo: null);
Both
query1
andquery2
returnsbar
andbaz
documents. But according to my understanding,query1
should have returnedbar
only.-
Dharmaraj almost 3 yearsI wasn't able to replicate this issue. This could be something with Flutter SDK then. Javascript SDK worked fine for me.
-
-
iDecode over 2 yearsBut if you remove
key
frombar
, then also it returns the same results. -
iDecode over 2 yearsI knew
isNull
part, and it works as intended but the question was whyisEqualTo: null
also returning"value"
documents. Even if there's nokey
, bothisEqualTo
andisNotEqualTo
returns same documents. If no answer is provided to my exact question, then I'll accept yours (although I already knew this thing) -
Diwyansh over 2 yearscan you add that to your question as well?
-
iDecode over 2 yearsI'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 over 2 yearsThanks, but can you point me to the docs where it says "isEqualTo: null" is not supported, I must have missed it.
-
Rémi Rousselet over 2 yearsI doubt there's docs about that. It's about how Dart and
where
works. Since Dart has noundefined
, doingisEqualTo:null
is identical to not passing it -
iDecode over 2 yearsOk, that made sense. I'm accepting your answer but will award the bounty to another user as he mentioned the working solution before you ;)