Danteon
Danteon

Reputation: 54

GestureDetector (Exception caught by gesture)

I'm trying to make a todo app. I create objects in card view, but when I press the delete icon on them when I first start the application, they do not delete the objects, they act as if I clicked the card and it gives this error. In the later ones, only the short text in red.

enter image description here enter image description here enter image description here

Expanded(
              child: ListView.builder(
                itemCount: allTodo.length,
                itemBuilder: (context, index) {
                  return Card(
                    child: ListTile(
                      onTap: () {
                        if (allTodo[index].ID == null) {
                          print("id is null, cant perform add operation");
                          return;
                        }
                        _controllerTitle.text = allTodo[index].title;
                        clickedTodoID = allTodo[index].ID!;
                        setState(() {});
                      },
                      title: Text(allTodo[index].title),
                      trailing: GestureDetector(
                        onTap: () {
                          if (allTodo[index].ID != null) {
                            _deleteTodo(allTodo[index].ID!, index);
                             setState(() {});
                          } else {
                            print("id is null, cant perform Delete operation");
                          }
                        },
                        child: Icon(Icons.delete),
                      ),
                    ),
                  );
                },
              ),
            ),

todo.dart

class Todo {
  int? ID;
  late String title;

  Todo(this.title);
  Todo.withId(this.ID, this.title);

  Map<String, dynamic> toMap() {
    var map = Map<String, dynamic>();
    map["ID"] = ID;
    map["title"] = title;
    return map;
  }

  Todo.fromMap(Map<String, dynamic> map) {
    this.ID = map["ID"];
    this.title = map["title"];
  }
}

Upvotes: 0

Views: 429

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63594

Most likely allTodo[index].ID is null here.

Try

onTap: () {
  if (allTodo[index].ID == null) {
    return;
  }
  setState(() {
    _controllerTitle.text = allTodo[index].title;
    clickedTodoID = allTodo[index].ID!;
  });
},

Use this snippet

return Card(
  child: ListTile(
    onTap: () {
      if (allTodo[index].ID == null) {
        print("id is null, cant perform add operation");
        return;
      }

      _controllerTitle.text = allTodo[index].title;
      clickedTodoID = allTodo[index].ID!;
      setState(() {});
    },
    title: Text(allTodo[index].title),
    trailing: GestureDetector(
      onTap: () {
        if (allTodo[index].ID != null) {
          _deleteTodo(allTodo[index].ID!, index);
          setState(() {});
        } else {
          print("id is null, cant perform Delete operation");
        }
      },
      child: Icon(Icons.delete),
    ),
  ),
);

Upvotes: 2

Related Questions