flutterdatatablesum

make a colum total in flutter datatable


i would very appreciate any one who can help. I wan to make a column in a table which is come from summarize of col 1 + col 2 + col 3 in every row i tried this code but the result is not summarize but likes concatenate from col 1 and col 2...

rows: List<DataRow>.generate(
                snapshot.data!.length,
                (index) {
                  var data = snapshot.data![index];
                  return DataRow(
                      color: MaterialStateColor.resolveWith((states) =>
                          index % 2 == 1
                              ? const Color.fromRGBO(34, 150, 243, 0.2)
                              : const Color.fromARGB(0, 140, 233, 148)),
                      cells: [
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(data.lapus1_n1), 0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                        ),
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(data.lapus2_n1), 0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                          //textAlign: TextAlign.right,
                        ),
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(data.lapus3_n1), 0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                        ),
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(
                                    data.lapus2_n1 + data.lapus3_n1),
                                0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                        ),
                      ]);
                },
              ).toList(),

the output is like this: enter image description here


Solution

  • change the below code like this and let me know,

     (Format.convertTo((double.tryParse(data.lapus2_n1) + double.tryParse(data.lapus3_n1)),0)),
    

    issues is you try to add the string then try to parse it. just parse both value then add.

    rows: List<DataRow>.generate(
                snapshot.data!.length,
                (index) {
                  var data = snapshot.data![index];
                  return DataRow(
                      color: MaterialStateColor.resolveWith((states) =>
                          index % 2 == 1
                              ? const Color.fromRGBO(34, 150, 243, 0.2)
                              : const Color.fromARGB(0, 140, 233, 148)),
                      cells: [
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(data.lapus1_n1), 0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                        ),
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(data.lapus2_n1), 0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                          //textAlign: TextAlign.right,
                        ),
                        DataCell(
                          Text(
                            (Format.convertTo(
                                double.tryParse(data.lapus3_n1), 0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                        ),
                        DataCell(
                          Text(
                             (Format.convertTo((double.tryParse(data.lapus2_n1) + double.tryParse(data.lapus3_n1)),0)),
                            style: const TextStyle(
                              color: Color.fromARGB(255, 17, 17, 17),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                          ),
                        ),
                      ]);
                },
              ).toList(),