Home > Software design >  DataTable rows doesn't update after deleting a row from SQLite flutter
DataTable rows doesn't update after deleting a row from SQLite flutter

Time:08-16

I am new to flutter and I made a SQLite database with data and I show the data in a Datatable. Whenever I select a row from the Datatable, a float action button appear to delete the row. After deleting the row, the Datatable doesn't update unless you leave the screen and comeback again.

The Datarow data is stored in a list from my SQLite class and I get it after reading all data from the Database.

Please help me!! I have been struggling with this problem for 4 days.

Is there a way to refresh the Datatable data or refresh the screen to update the Datatable?

Datatable Widget code:

  Widget builddatatable() {
    final coulmns = [
      '${sultandbfield.Customer_Name}',
      '${sultandbfield.Customer_Number}',
    ];
    return DataTable(
      columns: getColumns(coulmns),
      rows: getRows_2(notes),
    );
  }


  Future<List<SultanDB>> generate_data() async {
    var decode_data =
        json.decode("$full_json_data").cast<Map<String, dynamic>>();
    List<SultanDB> data_list =
        await decode_data.Map<SultanDB>((json) => SultanDB.fromJson(json))
            .toList();
    return data_list;
  }

  List<DataRow> getRows_2(List<SultanDB> sultandb) =>
      sultandb.map((SultanDB sultandb) {
        final cells = [sultandb.Customer_Name, sultandb.Customer_Number];
        return DataRow(
            selected: selected_data.contains(sultandb),
            onLongPress: () {
              Fluttertoast.showToast(msg: "${sultandb.Customer_ID}");
            },
            onSelectChanged: (isSelected) => setState(() {
                  final isAdding = isSelected != null && isSelected;
                  isAdding
                      ? {
                          selected_data.add(sultandb),
                          Float_appear(),
                        }
                      : {
                          selected_data.remove(sultandb),
                          Float_appear(),
                        };
                }),
            cells: getCells(cells));
      }).toList();

  List<DataCell> getCells(List<dynamic> cells) => cells
      .map((data) => DataCell(
            Text('${data}'),
          ),)
      .toList();

  List<DataColumn> getColumns(List<String> coulmns) => coulmns
      .map((String column) => DataColumn(
            label: Text(column),
          ))
      .toList();

and read all data to a list is notes<SultanDB> = await sultandata.instance.readalldata();

and delete customer onpress action:

List<int> user_ids = [];
        for(int i = 0 ; i < selected_data.length ; i  ) {
          user_ids.add(int.parse(selected_data[i].Customer_ID.toString()));
        }
        
        Delete_customer(user_ids);

CodePudding user response:

You should read data from SQLite after delete or just delete the specific data that you just remove from list that you hold the all data and then run setstate.

you can do this after delete data:

Delete_customer(user_ids);
var result = await sultandata.instance.readalldata();
setState(() {
   notes = result;
});
  • Related