[英]A dismissed Dismissible widget is still part of the tree. Getting this error repeatedly after removing item from list
[英]A dismissed Dismissible widget is still part of the tree. In flutter
我有一個使用 firebase 作為我的數據庫的應用程序,我在屏幕上遇到了這個問題
我正在添加 Dismissible 小部件以刪除項目。 當我查看但不適用於應用程序本身時,它可以在數據庫上正常工作。
這是我的解雇:
return Dismissible(
key: Key(todos[index].title),
background: Container(padding:EdgeInsets.only(left: 20),alignment: Alignment.centerLeft,
child:Icon(Icons.delete),
color:Colors.red,
),
onDismissed: (direction) async{
await DatabaseService().removeTodo(todos[index].uid);
},
child: ListTile(
onTap: (){
DatabaseService().completTask(todos[index].uid);
},
leading: Container(
padding: EdgeInsets.all(2),
height: 30,
width: 30,
decoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle
),
child: todos[index].isComplet? Icon(Icons.check,color: Colors.white):Container(
),
),
title: Text(
todos[index].title,
)
),
這是我的數據庫服務:
class DatabaseService{
CollectionReference todosCollection = FirebaseFirestore.instance.collection("Todos");
Future createNewTodo(String title) async{
return await todosCollection.add({
"title":title,
"isComplet":false,
});
}
Future completTask(uid)async{
await todosCollection.doc(uid).update({"isComplet":true});
}
Future removeTodo(uid) async{
await todosCollection.doc(uid).delete();
}
List<Todo> todoFromFirestore(QuerySnapshot snapshot){
return snapshot.docs.map((e) {
return Todo(uid: e.id,
title: (e["title"]),
isComplet: e["isComplet"],);
}).toList();
}
Stream<List<Todo>> listTodos() {
return todosCollection.snapshots().map(todoFromFirestore);
}
}
如果有人可以幫助我,我將不勝感激,我不知道為什么會發生此錯誤。
好的,所以經過一些研究,我自己解決了我的問題,問題肯定是索引,所以我改變了:
key: Key(todos[index].title),
為了:
key: UniqueKey(),
它工作正常,問題來自列表中已刪除的項目,小部件無法識別它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.