突然发生了“被关闭的可关闭小部件仍是树的一部分”错误FutureBuilder,ListView

问题描述

enter image description here

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();
  }

解决方法

哎,我也有同样的问题,我是这样解决的:修改key的值,使其成为唯一值。 如果你喜欢这样:key:Key(snap.data[index].id.toString()), 或者如果 voca.id 是一个字符串,你应该 key:Key(snap.data[index].id)