1

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!

SqueezeOJ
  • 441
  • 7
  • 17

2 Answers2

1

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 ...
        ],
  )
Obum
  • 1,485
  • 3
  • 7
  • 20
  • Thanks for your comment. My code works as written, I just can't figure out how to create n extra line each Sunday and then put a summation into it that new area. – SqueezeOJ Aug 05 '22 at 17:09
  • What do you mean by extra line? Do you mean an extra column? You might have to add the columns but then return empty cells for "Non-sunday" days. Does that make sense? The answer I posted was with regards the "if...else" error you got. I was surprised. – Obum Aug 06 '22 at 06:45
  • Adding another column is a good idea, but how do I sum employee hours inside the ```rows:[]``` section of this DataTable? – SqueezeOJ Aug 06 '22 at 19:51
  • 1
    Oh, you might have to use a method to do the computation outside the table, then call it with the appropriate data. If this doesn't make sense, add more context about employee hours sum in the question, like more code, the items data and maybe a graphic of what you want the table to look like, let's come up with something – Obum Aug 07 '22 at 13:36
0

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
SqueezeOJ
  • 441
  • 7
  • 17