# Flutter 踩坑實錄

[toc]

Another exception was thrown: A dismissed Dismissible widget is still part of the tree.

具體代碼爲

ListView.builder(
              itemBuilder: (context, index) {
                var item = _list[index];
                debugPrint('the key is ${index.toString()}');
                return Dismissible(
                  key: Key(item.id.toString()),
                  confirmDismiss: (DismissDirection direction) async {
                    final bool res = await showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return CupertinoAlertDialog(
                            title: Text(
                              "注意",
                              style: TextStyle(
                                  fontSize: 16, color: ColorConf.color000000),
                            ),
                            content: const Text("您是否要取消收藏這個文章呢?"),
                            actions: <Widget>[
                              FlatButton(
                                  onPressed: () {
                                    Navigator.of(context).pop(true);
                                    _deleteItem(item.id, item.originId, index);
                                  },
                                  child: Text(
                                    "是的",
                                    style: TextStyle(
                                        fontSize: 14,
                                        color: ColorConf.colorGreen),
                                  )),
                              FlatButton(
                                onPressed: () =>
                                    Navigator.of(context).pop(false),
                                child: Text(
                                  "CANCEL",
                                  style: TextStyle(
                                      fontSize: 14,
                                      color: ColorConf.color48586D),
                                ),
                              )
                            ],
                          );
                        });
                    return res;
                  },
                  background: Container(
                    padding: const EdgeInsets.only(right: 30),
                    alignment: Alignment.centerRight,
                    color: Colors.red,
                    child: Text(
                      '滑動取消收藏',
                      style:
                          TextStyle(fontSize: 16, color: ColorConf.colorFFFFFF),
                    ),
                  ),
                  child: Container(
                      child: Column(
                    children: <Widget>[
                      ProjectWidget.renderListViewCollectionItem(
                          _list[index], () {}),
                      Divider(),
                    ],
                  )),
                );
              },
              itemCount: _list.length)

具體表現爲:刪除了某個item後再去下拉更新數據,會報錯,可能是因爲key的問題,所以修改爲:

  key: Key(UniqueKey().toString()),

表現良好

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章