Skip to content

🐛 [firestore] Not able to filter/query on fields that are null #11874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
puf opened this issue Nov 12, 2023 · 9 comments · Fixed by #11896
Closed

🐛 [firestore] Not able to filter/query on fields that are null #11874

puf opened this issue Nov 12, 2023 · 9 comments · Fixed by #11896
Assignees
Labels
plugin: cloud_firestore resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working

Comments

@puf
Copy link
Contributor

puf commented Nov 12, 2023

Bug report

I have a query that tries to retrieve documents with a (date) field that has a null value.

const query = FirebaseFirestore.instance.collection("77468864").where("date", isEqualTo: null);
var snapshot = await query.get();
print('Got snapshot with ${snapshot.size} documents');

The Flutter code (when run on web, I didn't test other platforms) incorrectly returns documents with an actual date value, while the corresponding JavaScript code does not return these documents.

Steps to reproduce

  1. I have a collection with one document that has two fields (date and string), both of which have a value.
  2. Go to https://zapp.run/edit/firestore-query-null-date-zr1q06yyr1r0
  3. Check the log output to see that the code returns a document with a non-null date field
  4. Go to https://stackblitz.com/edit/firestore-v9-query-null-date?file=index.js
  5. Check the log output to see that the code returns no documents

Expected behavior

I expect the Flutter and JavaScript projects to show the same result, given that they run an equivalent query on the same data.


Additional context

Originally reported here: https://stackoverflow.com/q/77468864

@puf puf added type: bug Something isn't working Needs Attention This issue needs maintainer attention. labels Nov 12, 2023
@Ehesp
Copy link
Member

Ehesp commented Nov 12, 2023

I believe there's a specific isNull named argument for this. The reason was that there's no concept of undefined in Dart, so it had to be specific when you want to filter by null

@puf
Copy link
Contributor Author

puf commented Nov 13, 2023

Thanks Elliot!
I should've known this, especially since I'm pretty sure I used the isNull overload in the past.

Undefined and null are different concepts though. Why can't the Flutter wrapper recognize the null value that I used in my testbed: .where("date", isEqualTo: null)?

@danagbemava-nc danagbemava-nc added the triage Issue is currently being triaged. label Nov 13, 2023
@danagbemava-nc
Copy link

Hi @puf, should this be treated as a feature request?

@danagbemava-nc danagbemava-nc added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Nov 13, 2023
@russellwheatley
Copy link
Member

Hey @puf, you have to use isNull named argument in the query because we check the value in queries here:

if (isEqualTo != null) addCondition(field, '==', isEqualTo);
if (isNotEqualTo != null) addCondition(field, '!=', isNotEqualTo);

If the value is null, it does not become part of the query (as you can see in the link above). There really is no way around this as the API is currently implemented. We can't check whether the named arg was intentionally set as null by the user or unintentionally set (i.e. it is a nullable named argument).

Firestore doesn't allow undefined values set in the documents, the concept of undefined is a quirk of JavaScript and is not cross platform compatible as far as I'm aware.

@puf
Copy link
Contributor Author

puf commented Nov 13, 2023

Thanks for clarifying Russel. Do any of our SDKs for other languages drop null values like that? Or is there something idiomatic Flutter about dropping null value?

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Nov 13, 2023
@russellwheatley
Copy link
Member

It is more an issue with the way the API was initially designed. In an ideal world, the operator and the value would be separate arguments. This would allow null values to be used on queries for isEqualTo & isNotEqualTo. But, because the operator and the value are set as one named argument, we cannot ascertain whether a null value was set by the user or not for that particular named argument (i.e. isEqualTo & isNotEqualTo).

@russellwheatley
Copy link
Member

Hmmm, I've done a little bit of digging into this and there might be a way around it. I'll see what we can do 🤔

@russellwheatley russellwheatley self-assigned this Nov 14, 2023
@danagbemava-nc danagbemava-nc added plugin: cloud_firestore and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Nov 15, 2023
@Ehesp
Copy link
Member

Ehesp commented Nov 17, 2023

Could we make the default values of each argument a const/symbol which is internal, and if it's that value, skip it?

@russellwheatley
Copy link
Member

@danagbemava-nc danagbemava-nc added the resolution: fixed A fix has been merged or is pending merge from a PR. label Nov 22, 2023
@firebase firebase locked and limited conversation to collaborators Dec 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: cloud_firestore resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants