【问题标题】:GestureDetector (Exception caught by gesture)GestureDetector(手势捕获的异常)
【发布时间】:2022-08-21 01:37:08
【问题描述】:

我正在尝试制作一个待办事项应用程序。我在卡片视图中创建对象,但是当我第一次启动应用程序时按下它们上的删除图标时,它们不会删除对象,它们的行为就像我单击了卡片并给出了此错误。在后面的,只有红色的短文本。

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\"];
  }
}

    标签: flutter dart dart-null-safety


    【解决方案1】:

    allTodo[index].ID 很可能在这里为空。

    尝试

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

    使用这个sn-p

    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),
        ),
      ),
    );
    

    【讨论】:

    • 再次发生错误error
    • 你有没有像我一样返回案件。您能否包含将重现相同问题的完整小部件,更多关于minimal-reproducible-example
    • 把 setState 放在最后,也做尾随,如果不起作用,考虑包括完整的小部件
    • 抱歉我没明白你的意思
    猜你喜欢
    • 2020-11-01
    • 2021-05-23
    • 2021-08-20
    • 2022-06-11
    • 2022-07-07
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多