[Flutter]使用Dismissible组件实现滑动删除过程遇到“A dismissed Dismissible widget is still part of the tree.”
freddon
发表于2019-08-12
阅读 2337 |
评论 0
“滑动删除”模式,在ListItem组件外层包裹Dismissible组件,滑动删除操作后,报错: A dismissed Dismissible widget is still part of the tree.
核心代码如下:
__ListItem:__
```
Widget _itemView(Unit item) => ListTile(
title: Text(
item.name,
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.w700, fontSize: 17.0),
),
leading: ClipRRect(
borderRadius: BorderRadius.circular(12.0),
child: Image.asset(
'assets/icon/' + item.icon,
width: 48,
height: 48,
)),
trailing: Icon(
Icons.chevron_right,
color: Colors.orange,
size: 24,
),
);
```
__Dismissble部分:__
```
Dismissible(
key: new Key(item.name),
background: new Container(color: Colors.red),
secondaryBackground: Container(
color: Colors.green,
),
child: _itemView(item),
onDismissed: (direction) {
removeItem(item);
})
```
如上所示,没一个ListItem绑定的对象为Unit类型,至少有`name`、`icon`属性。
当执行滑动删除时,报错如下:
```
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown building Dismissible-[<'DNF'>](dirty, dependencies:
flutter: [Directionality], state: _DismissibleState#c1bbf(tickers: tracking 2 tickers)):
flutter: A dismissed Dismissible widget is still part of the tree.
flutter: Make sure to implement the onDismissed handler and to immediately remove the Dismissible
flutter: widget from the application once that handler has fired.
```
即:__`该widget并未从节点中移除`__
排查步骤:
1. 检查`removeItem(item);`方法,排查是否从提供的数据源中移除该条目;
2. 检查item对象`(Unit)`的Key是否唯一。即:
```
Dismissible(
key: new Key(item.name),
//....
)
```
果然,item.name在本例中会重复,采用`new ObjectKey(item)`或`new Key(item.id)`即可。
以上。
分类 :日常记录