SQLite删除方法在Flutter中也不起作用。

huangapple go评论62阅读模式
英文:

the sqlite deletion method does not work in flutter either

问题

以下是您要翻译的内容:

i'm making a todo app using sqlite deleteSqlite, it doesn't delete when the button is pressed during the deletion process, but I wonder why it deletes when it does hot resart?

Future<void> _deleteTodoItem(int id, String text, bool isDone) async {
  await TodoModelDao().deleteTodo(id, text, isDone);
  refleshList();
}
Future<List<TodoModel>> _showAllData() async {
  var todoList = await TodoModelDao().getAllData();
  return todoList;
}

refleshList() {
  setState(() {
    _showAllData();
  });
}
Future<void> deleteTodo(int id, String text, bool isDone) async {
  var db = await DatabaseHelper.dbAccess();

  await db.delete(TABLENAME, where: "$ID = ?", whereArgs: [id]);
}
Future<List<TodoModel>> getAllData() async {
  var db = await DatabaseHelper.dbAccess();

  List<Map<String, dynamic>> maps = await db.rawQuery("SELECT * FROM $TABLENAME");

  return List.generate(maps.length, (i) {
    var row = maps[i];
    return TodoModel(id: row[ID], text: row[TEXT]);

  });
}
child: IconButton(
  onPressed: () {
    _deleteTodoItem(widget.todoModel.id, widget.todoModel.text,
        widget.todoModel.isDone);
  },
  icon: const Icon(Icons.delete),
  color: Coloors.whiteColor,
  iconSize: 18,
),
英文:

i'm making a todo app using sqlite deleteSqlite, it doesn't delete when the button is pressed during the deletion process, but I wonder why it deletes when it does hot resart?

  Future<void> _deleteTodoItem(int id, String text, bool isDone) async {
    await TodoModelDao().deleteTodo(id, text, isDone);
    refleshList();
  }
  Future<List<TodoModel>> _showAllData() async {
    var todoList = await TodoModelDao().getAllData();
    return todoList;
  }

  refleshList() {
    setState(() {
      _showAllData();
    });
  }

  Future<void> deleteTodo(int id, String text, bool isDone) async {
    var db = await DatabaseHelper.dbAccess();

    await db.delete(TABLENAME, where: "$ID = ?", whereArgs: [id]);
  }
  Future<List<TodoModel>> getAllData() async {
    var db = await DatabaseHelper.dbAccess();

    List<Map<String, dynamic>> maps = await db.rawQuery("SELECT * FROM $TABLENAME");

    return List.generate(maps.length, (i) {
      var row = maps[i];
      return TodoModel(id: row[ID], text: row[TEXT]);

    });
  }
child: IconButton(
            onPressed: () {
              _deleteTodoItem(widget.todoModel.id, widget.todoModel.text,
                  widget.todoModel.isDone);
            },
            icon: const Icon(Icons.delete),
            color: Coloors.whiteColor,
            iconSize: 18,
          ),

答案1

得分: 1

删除操作已经执行,但是Flutter不知道你想要在删除后重新渲染UI。
当你热重启时,它会重新渲染。有许多方法可以解决这个问题。
一个简单的解决方案是调用 setState 强制重新渲染。
你可以尝试将你的代码的某些部分(IconButton)替换为以下内容:

child: IconButton(
    onPressed: () async {
        await _deleteTodoItem(widget.todoModel.id, widget.todoModel.text,
            widget.todoModel.isDone);
        // 强制重新渲染UI
        setState(() {
            // 在这里更新你的变量...
            _showAllData();
        });
    },
    icon: const Icon(Icons.delete),
    color: Coloors.whiteColor, // 这里的拼写可能有误,应该是Colors.whiteColor
    iconSize: 18,
),
英文:

It does delete, but flutter is unaware that you want to re-render ui after deletion.<br>
When you hot restart, it re-renders it.There are many work around this. <br>One simple solution would be calling setState to force re-render.<br>
You can try replacing some part of your code (IconButton) with the following.

child: IconButton(
            onPressed: () async {
             await _deleteTodoItem(widget.todoModel.id, widget.todoModel.text,
                  widget.todoModel.isDone);
              // force re-render ui
              setState(() {
                // update your variable here...
                 _showAllData();
             });
            },
            icon: const Icon(Icons.delete),
            color: Coloors.whiteColor,
            iconSize: 18,
          ),

huangapple
  • 本文由 发表于 2023年6月16日 05:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定