首页 > 解决方案 > LIstView 中的 Flutter Dismissible 事件

问题描述

我已经在onDismissed:函数中实现了一个删除项目方法。

此方法仅适用于列表的最后一项。

如果我删除任何其他项目,我会收到以下错误:

已关闭的 Dismissible 小部件仍然是树的一部分。

复杂之处在于我使用地图来保存和删除我需要的所有事件。仍然在调试模式下,索引似乎很好。

错误出现在此 setState 方法之后:

setState(() {
              print('_controller.selectedDay = ${_controller.selectedDay}');
              customEvents[_controller.selectedDay].removeAt(index);
            });

这是代码部分:

Widget myCalendarBuild(BuildContext context, List<dynamic> shiftList) {
    return ListView.separated(
      separatorBuilder: (BuildContext context, int index) =>
          Divider(color: Colors.black),
      itemCount: _listOfShiftsPerGivenDay.length,
      itemBuilder: (BuildContext context, int index) {
        return Dismissible(
          key: Key(customEvents[_controller.selectedDay][index].toString()),
          onDismissed: (direction){
//            removeShift(index);
            Shift deleteShift = getParametersFromWidgetInTheList(index: index);
            // remove from database
            DatabaseProvider.db.delete(deleteShift.id).then((_) =>
                BlocProvider.of<ShiftBloc>(context)
                    .add(DeleteShift(deleteShift.id)));
            setState(() {
              print('_controller.selectedDay = ${_controller.selectedDay}');
              customEvents[_controller.selectedDay].removeAt(index);
            });
            showSnackBar(context);
          },
          child: Container(
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 3,
                  child: customEvents[_controller.selectedDay][index],
                ),
                Expanded(
                  flex: 1,
                  //fixme: catch when the user clicks back instead of submit when editing/ ading a shift
                  child: GestureDetector(
                    onTap: () {
                      Shift shift =
                          getParametersFromWidgetInTheList(index: index);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (BuildContext context) => ShiftForm(
                            shift: shift,
                          ),
                        ),
                      );
                    },
                    child: Container(
                      child: Icon(FontAwesomeIcons.edit),
                      color: Colors.teal,
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  //fixme: catch when the user clicks back instead of submit when editing/ ading a shift
                  child: GestureDetector(
                    onTap: () {
                      Shift deleteShift =
                          getParametersFromWidgetInTheList(index: index);
                      // remove from database
                      DatabaseProvider.db.delete(deleteShift.id).then((_) =>
                          BlocProvider.of<ShiftBloc>(context)
                              .add(DeleteShift(deleteShift.id)));
                      // remove from customEvents Map
                      setState(() {
                        customEvents[_controller.selectedDay].removeAt(index);
                        showSnackBar(context);
                      });
                      // after deleting the item I have to refresh the list
//                    calendarBuild(context, shiftList);
                    },
                    child: Container(
                      child: Icon(FontAwesomeIcons.trash),
                      color: Colors.red,
                    ),
                  ),
                )
              ],
            ),
            //                  ),
          ),
        );
      },
    );
  }

在这里,您可以看到我向左滑动以删除事件时发生的情况。也标记为黄色:

在此处输入图像描述

标签: flutter

解决方案


发现问题了!

它发生在以下行:

key: Key(customEvents[_controller.selectedDay][index].toString()),

我只需要用以下内容替换它:

key: UniqueKey(),

问题可能发生在地图中。


推荐阅读