【问题标题】:`A dismissed Dismissible widget is still part of the tree` error occurred suddenly (FutureBuilder, ListView)`一个被解雇的 Dismissible 小部件仍然是树的一部分`错误突然发生(FutureBuilder,ListView)
【发布时间】:2020-08-16 13:35:25
【问题描述】:

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

【问题讨论】:

  • voca.toString() 返回什么?
  • @Nitrodon ???什么意思?

标签: flutter dart dismissible


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 2020-12-19
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多