Megha Choudhary
Megha Choudhary

Reputation: 1

Unable to Delete Task from Database

I am trying to delete a task from the screen of my app. I have created two tables for the same task: in the first table, I am storing just the ID and name of the task, and in the second table, I am doing the main work by storing the ID from the first table along with the tracked date. The issue I am facing is that when I try to delete the task from the app by swiping right to left, the task is efficiently deleted from the tasks table, but it should happen in such a way that when I try to delete the task from the app, it should be deleted from both tables (tasks table and task_tracked table). Currently, only the task is being deleted from the tasks table, and the tracked table is not affected. However, these tables are interlinked, and any activity performed should affect both tables.

//dbhelper class
 Future<void> deleteTrackTask(int taskId, String trackedDate) async {
    final db = await database;

    await db.delete(
      tableTaskTrack, //for tracking the date table 
      where: '$columnTaskId = ? AND $columnTrackedDate = ?',
      whereArgs: [taskId, trackedDate],
    );
  }

  Future<void> deleteTask(int taskId) async {
    final db = await database;
    await db.delete(
      tableTasks,
      where: '$columnId = ?',
      whereArgs: [taskId],
    );
  }
//controller class
void deleteTrackTask(int index) async {
    try {
      int taskId = taskIdList[index];
      String trackedDate = DateFormat('dd-MM-yyyy').format(DateTime.now());

      await _databaseHelper.deleteTrackTask(taskId, trackedDate);

      //isTaskSelected.removeAt(index);
    } catch (e) {
      print("Error deleting task: $e");
    }
  }

  void deleteTask(int index) async {
    try {
      int taskId = taskIdList[index];

      await _databaseHelper.deleteTask(taskId);
      taskList.removeAt(index);
      //isTaskSelected.removeAt(index);
    } catch (e) {
      print("Error deleting task: $e");
    }
  }

Upvotes: 0

Views: 60

Answers (1)

Bram
Bram

Reputation: 680

Your selection in tableTaskTrack tries to find a row that also has its trackedDate set to now() and such a row will not exist.

Your selection in the tableTaskTrack should select only on taskId, ignoring the date. So, change:

where: '$columnTaskId = ? AND $columnTrackedDate = ?',
whereArgs: [taskId, trackedDate],

to

where: '$columnTaskId = ?',
whereArgs: [taskId],

Upvotes: 0

Related Questions