skip to Main Content

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


  1. 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 the in 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:

    workmonths: ['202207', '202208']
    

    So there’s a value in workmonths for any month that exists in the workdates 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 the arrayUnion operation.

    Now you can find the documents where there were work dates in July with:

    collRef.where("workmonths", arrayContains: "202207");
    

    Or the documents with work dates in July and August with:

    collRef.where("workmonths", arrayContainsAny: ["202207", "202208"]);
    
    Login or Signup to reply.
  2. assuming you already fetched the employee model and you have selected a month, you can do something like this:

    List<WorkDate> getEmployeeWorkDates(Employee employee, DateTime selectedMonth) {
      final nextMonth = DateTime(month.year, selectedMonth.month + 1);
    
      return employee.workDates
          .where(
            (workDate) =>
                workDate.date.isAfter(selectedMonth) && workDate.date.isBefore(nextMonth),
          )
          .toList();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search