flutter - 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,
),
),
)
],
),
// ),
),
);
},
);
}
在这里,您可以看到我向左滑动以删除事件时发生的情况。也标记为黄色:
解决方案
发现问题了!
它发生在以下行:
key: Key(customEvents[_controller.selectedDay][index].toString()),
我只需要用以下内容替换它:
key: UniqueKey(),
问题可能发生在地图中。
推荐阅读
- java - 将 2D 输入数组转换为非平衡 BST
- c++ - Ns3 - 如何通过 CCH 通道(wave-simple-device.cc 编辑)定期在 VANET 中发送 WSMP 数据包?
- swift - 使用组合框架的按钮操作
- python - Python 3.7 tkinter 8 - 即时更改菜单项“命令”选项
- html - 无法在导航栏 html 中显示徽标
- swift - Swift Decodable,Endpoint 返回完全不同的类型
- ios - 如何确定我的应用程序是否包含加密?
- spring-data-cassandra - 使用 spring-data-cassandra 在 cassandra 中进行分页和排序
- android - Android Studio 启动活动
- java - 谷歌使用 Firebase 登录时的唯一 ID