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!
Your code seems correct.
What if you took the if outside the list and use ternary instead. Something like: