gpt4 book ai didi

sqlite - 被解雇的 Dismissible 小部件仍然是 flutter 树的一部分

转载 作者:行者123 更新时间:2023-12-03 03:16:01 33 4
gpt4 key购买 nike

我有一个使用 provider 的待办事项应用程序用于状态管理和 sqlite数据库。

在应用程序中,我尝试添加 Dismissible小部件删除项目。

但问题是当我尝试删除项目时它确实从 database 中删除了但我在屏幕上出现错误。我是新手。

dismissible image

error in console.


════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building Dismissible-[<'howll'>](dirty, dependencies: [Directionality], state: _DismissibleState#98163(tickers: tracking 2 tickers)):
A dismissed Dismissible widget is still part of the tree.

Make sure to implement the onDismissed handler and to immediately remove the Dismissible
widget from the application once that handler has fired.
User-created ancestor of the error-causing widget was:
TaskListTile file:///C:/Users/adity/Desktop/flutter-app/todoye/lib/widgets/task_list_view.dart:14:22
When the exception was thrown, this was the stack:
#0 _DismissibleState.build.<anonymous closure> (package:flutter/src/widgets/dismissible.dart:526:11)
#1 _DismissibleState.build (package:flutter/src/widgets/dismissible.dart:533:8)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4047:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3941:15)
#4 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
...

这是我的代码。

task_list_tile.dart


Dismissible(
key: Key(taskTitle),
onDismissed: (direction) {
deleteCallback();
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text("$taskTitle removed.")));
},
child: ListTile(
title: Text(
'$taskTitle',
),

task_data.dart


void deleteTask(int id) {
taskDatabaseManager.deleteTask(id);
notifyListeners();
}

databse_connection.dart


Future<void> deleteTask(int id) async {
await openDb();
await _database.delete(
'tasks',
where: 'id = ?',
whereArgs: [id],
);
}

task_list_view.dart


class TaskListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<TaskData>(
builder: (context, taskData, child) {
return ListView.builder(
itemCount: taskData.taskCount,
itemBuilder: (context, index) {
return TaskListTile(
taskTitle: taskData.tasks[index].name,
isChecked: taskData.tasks[index].isDone,
checkboxCallback: (checkboxState) {
taskData.updateTask(taskData.tasks[index]);
},
deleteCallback: () {
taskData.deleteTask(taskData.tasks[index].id);
},
);
});
},
);
}
}

最佳答案

如果您确定您的可关闭小部件已被删除。尝试修复 key .
键应该是唯一的。如果任务的 id 是唯一的,

key: Key(taskData.tasks[index].id)
如果您没有任何其他恒定且唯一的数据,则可以尝试
key: UniqueKey()

关于sqlite - 被解雇的 Dismissible 小部件仍然是 flutter 树的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470821/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com