簡體   English   中英

`一個被解雇的 Dismissible 小部件仍然是樹的一部分`錯誤突然發生(FutureBuilder,ListView)

[英]`A dismissed Dismissible widget is still part of the tree` error occurred suddenly (FutureBuilder, ListView)

在此處輸入圖像描述

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM