'Firestore null query returns non-nullable field

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.



Solution 1:[1]

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.

Solution 2:[2]

There's nothing wrong in returned data by your queries because

var query1 = collection.where('key', isEqualTo: null);

In query1 basically you are running a query like - Select * from collection where 'key' = null and same in the query2.

And because bar has a key that has a null value your query1 is true and returning the data.

var query2 = collection.where('key', isNotEqualTo: null); 

And same scenario is happening with above query2 it also has a key that is not equals to `null. So it's also returning you the available data.

So because both of your where conditions are getting satisfied that's why they are returning you the available data.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 Diwyansh