我有一个待办事项应用程序,它使用provider
进行状态管理和sqlite
数据库。
在该应用中,我试图添加Dismissible
小部件以删除项目。
但是问题是当我尝试删除项目时,它确实从database
中删除了,但是屏幕上出现错误。我是新手。
控制台错误
════════ 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);
},
);
});
},
);
}
}
答案 0 :(得分:4)
如果确定您的可删除小部件已删除。尝试修复key
。
密钥应该是唯一的。如果任务的ID是唯一的,
key: Key(taskData.tasks[index].id)
如果您没有其他常量和唯一数据,则可以尝试
key: UniqueKey()
答案 1 :(得分:1)
关闭Dismissible
时,应将其从小部件树中删除。对于您的情况,您正在为Dismissible
的每个项目显示一个taskData
,因此,如果您取消某个项目的Dismissible
,则应从列表中删除该项目。
因此,在deleteCallback
中,完成deleteTask()
后,您应该在taskData.removeAt(index)
内进行setState()
。