How to sum rows in Flutter Data Table after every week of data?

656 Views Asked by At

I'm writing a simple timeclock for my employees in Flutter.

I display their timecard in a Flutter Data Table, like this:

  DataTable(

    columns: const [
      DataColumn(label: Text('Employee')),
      DataColumn(label: Text('Clock-In')),
      DataColumn(label: Text('Clock-Out')),
      DataColumn(label: Text('Hours')),
      DataColumn(label: Text('Tips')),
    ],

    rows: [

      for (var item in _listTimesheet.reversed) DataRow(cells:
      [
        // TODO: If Sunday Then Display Sunday AND Sum of Hours
        if (DateFormat('EEEE').format(DateTime.parse(item["CLOCKED_IN_TIME"].toString())) == "Sunday")...[
          DataCell(Text(item["EMPLOYEE_NAME"].toString())),
          DataCell(Text("${DateFormat('yMEd').format(DateTime.parse(item["CLOCKED_IN_TIME"].toString()))}\n${DateFormat('jm').format(DateTime.parse(item["CLOCKED_IN_TIME"].toString()))}" )),
          DataCell(Text("${DateFormat('yMEd').format(DateTime.parse(item["CLOCKED_OUT_TIME"].toString()))}\n${DateFormat('jm').format(DateTime.parse(item["CLOCKED_OUT_TIME"].toString()))}" )),
          DataCell(Text(item["HOURS"].toString())),
          DataCell(Text(item["TIPS"].toString())),
          // TODO: Add Extra Row with Sum of Hours
          // TODO: Then Reset Sum
        ] else...[
          // TODO: Not Sunday, so Display Info and Add to Sum
          DataCell(Text(item["EMPLOYEE_NAME"].toString())),
          DataCell(Text("${DateFormat('yMEd').format(DateTime.parse(item["CLOCKED_IN_TIME"].toString()))}\n${DateFormat('jm').format(DateTime.parse(item["CLOCKED_IN_TIME"].toString()))}" )),
          DataCell(Text("${DateFormat('yMEd').format(DateTime.parse(item["CLOCKED_OUT_TIME"].toString()))}\n${DateFormat('jm').format(DateTime.parse(item["CLOCKED_OUT_TIME"].toString()))}" )),
          DataCell(Text(item["HOURS"].toString())),
          DataCell(Text(item["TIPS"].toString())),
          // TODO: Add to Sum
        ]

      ]
      )
    ],

  )

As you can see, I'd like to display the sum the hours for the week every Sunday. However the ELSE statement gives this error: The element type 'List<DataCell>' can't be assigned to the list type 'DataCell'..

How can I sum my hours weekly and display them inside this Data Table?

Thanks!

2

There are 2 best solutions below

4
On

Your code seems correct.

What if you took the if outside the list and use ternary instead. Something like:

for (var item in _listTimesheet.reversed) DataRow( 
  cells: DateFormat('EEEE')
    .format(DateTime.parse(item["CLOCKED_IN_TIME"].toString())) == "Sunday"
      ? [ 
          // DataCells ...
        ]
      : [
          // DataCells ...
        ],
  )
0
On

I've been working on this a lot and I now realize that answering this seems to have more to do with structuring the data I display in the DataTable, rather than trying to force the DataTable into doing so much for me.

The way I solved this situation using a few techniques:

  1. Make sure that every week has all 7 days in it even if the person didn't work every day. So that the DataTable now contains at least 7 rows for every week.

  2. Null check each day, since there would now be empty days in the DataTable. Using a ternary like this:

DataCell(
    item["EMPLOYEE_ID_LINK"] == null ? const Text(" ") :
    Text(item["TIPS"].toString())
),
  1. Add another DataRow after the For loop in which to display the totals, like this:
rows: [

   for (var item in _listTimesheet.reversed) DataRow(cells:
      [
        // Daily DataCells - No If Then Else Statement
      ],
   
   DataRow(cells:
      [
        // Totals Row DataCells
      ] 

   ]
  1. Write code to get totals for the 7 day period and populate that into the extra row on the DataTable