[英]A dismissed Dismissible widget is still part of the tree in flutter
I have a todo app which uses provider
for state management and sqlite
database.我有一个 todo 应用程序,它使用
provider
进行 state 管理和sqlite
数据库。
In the app I am trying to add Dismissible
widget to delete item.在应用程序中,我尝试添加
Dismissible
小部件以删除项目。
But the problem is when I try to delete item It does get deleted from database
but I get error on the screen.I am new to flutter.但问题是当我尝试删除项目时它确实从
database
中删除但我在屏幕上出现错误。我是 flutter 的新手。
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)
...
This is my code.这是我的代码。
task_list_tile.dart
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
task_data.dart
void deleteTask(int id) {
taskDatabaseManager.deleteTask(id);
notifyListeners();
}
databse_connection.dart
数据库连接.dart
Future<void> deleteTask(int id) async {
await openDb();
await _database.delete(
'tasks',
where: 'id = ?',
whereArgs: [id],
);
}
task_list_view.dart
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);
},
);
});
},
);
}
}
If you are sure about the your dismissible widget is removed.如果您确定您的可关闭小部件已被删除。 Try to fix
key
.尝试修复
key
。 Key should be unique.密钥应该是唯一的。 If tasks's id is unique,
如果任务的 id 是唯一的,
key: Key(taskData.tasks[index].id)
If you didn't have any other constant and unique data, you could try如果您没有任何其他恒定且唯一的数据,您可以尝试
key: UniqueKey()
When you dismiss a Dismissible
, you should remove it from the widget tree.当您关闭
Dismissible
时,您应该将其从小部件树中删除。 In your case, you are showing a Dismissible
for each item of taskData
, so if you dismiss the Dismissible
of an item you should remove that item from the list.在您的情况下,您为
Dismissible
的每个项目显示taskData
,因此如果您关闭项目的Dismissible
,则应从列表中删除该项目。
So, in the deleteCallback
, after doing deleteTask()
, you should do taskData.removeAt(index)
inside setState()
.因此,在
deleteCallback
中,在执行deleteTask()
之后,您应该在setState()
中执行taskData.removeAt(index)
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.