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.
我已经知道在 stackoverflow 中有很多关于这个问题的问题,并且也几乎阅读了其中的一些问题。但我不知道为什么会发生这种情况,因为我没有 setState 问题,而且可关闭的键也是正确的。你能找到我错过的问题吗?有人说不Dismissible
应该在ListView
小部件中,但直到昨天它甚至在ListView
. 我试过key: UniqueKey()
了,但没有用。如果您知道任何解决方案,请告诉我。提前致谢。
Widget vocaBuilder() {
return FutureBuilder(
future: loadTodayVoca(),
builder: (context, snap) {
if (snap.data.length == 0 || snap.data.isEmpty) {
return Container();
} else {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: snap.data.length,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
Voca voca = snap.data[index];
return GestureDetector(
onTap: () {
editPage(voca.id);
},
child: Dismissible(
direction: DismissDirection.endToStart,
background: Container(
color: Colors.red,
padding: EdgeInsets.only(top: 23, right: 30)),
key: Key(snap.data[index].toString()),
...
onDismissed: (direction) {
setState((){
deleteVoca(voca.id);
snap.data.removeAt(index);
}); }));
Future<void> deleteVoca(String id) async {
DBHelper sd = DBHelper();
await sd.deleteVoca(id);
}
Future<List<Voca>> loadTodayVoca() async {
DBHelper sd = DBHelper();
var list = await sd.vocas();
return list
.where((list) =>
list.createTime ==
DateFormat('yyyy-MM-dd')
.format(DateTime(now.year, now.month, now.day)))
.toList();
}