This is my cloud firestore data:
I want to bring the dates in the workdates list according to the selected month in the application, but I can’t make the code in flutter, can you help me somehow?
This is my cloud firestore data:
I want to bring the dates in the workdates list according to the selected month in the application, but I can’t make the code in flutter, can you help me somehow?
2
Answers
There is no way to query the
workdates
array field of your documents for just the month of the dates. The closest Firestore has is thein
operator, but for that you need to specify the entire array item and not just one of the subfields in there.The simplest workaround I can think of is to add an additional top-level array field to your document where you just the months of the
workdates
for that document in a queryable format, so for example:So there’s a value in
workmonths
for any month that exists in theworkdates
field, but only one entry even when there are multiple dates within that month. To prevent duplicates, be sure to write to this field with thearrayUnion
operation.Now you can find the documents where there were work dates in July with:
Or the documents with work dates in July and August with:
assuming you already fetched the employee model and you have selected a month, you can do something like this: