[英]How to resolve issue: “A dismissed Dismissible widget is still part of the tree” while use Bloc
while implementing Dismissible
widget I have error while removing an item.在实现
Dismissible
小部件时,我在删除项目时出错。
return Dismissible(
key: Key(widget.product.id),
onDismissed: (direction) {
setState(() {
BlocProvider.of<ManagerBloc>(context)
.add(RemoveProduct(widget.product));
});
Parent of this child looks like这个孩子的父母看起来像
return ListView.builder(
itemCount: state.shopItem.length,
itemBuilder: (BuildContext context, int index) {
return ProductElement(product: state.shopItem[index]);
});
}
I moved it to parent and remove shopItem.removeAt()
with blocProvider but still I got this isseu.我将它移到父级并使用 blocProvider 删除
shopItem.removeAt()
但我仍然得到了这个问题。 Even if I remove the object using remove
on the list, it's showing this same error message:即使我在列表中使用
remove
删除了 object,它也会显示相同的错误消息:
if (event is RemoveProduct) {
await shopListRepository.remove(event.product);
yield DefaultDataManager((state as DefaultDataManager)
.shopItem
.where((item) => item.id != event.product.id)
.toList());
}
I tried UniqueKey and it was this same result.我尝试了 UniqueKey,结果是一样的。 My product_id is '64b7ff60-f782-11e9-a3e8-a9ee0aa87ea5' generated by uuid.v1().
我的 product_id 是由 uuid.v1() 生成的 '64b7ff60-f782-11e9-a3e8-a9ee0aa87ea5'。
I think the issue is that you are not removing the data model whose list generates the UI just after (synchronously) onDismissed
was called.我认为问题在于您没有删除数据 model ,其列表在调用
onDismissed
之后(同步)生成 UI。 Let us say you have a List<Item> _items
in your widget's State
that stores the UI data model that populates the list.假设您在小部件的
State
中有一个List<Item> _items
,它存储填充列表的 UI 数据 model。 What you have to do in order for Dismissible
to work is to call _items.remove(item)
in your State
synchronously when onDismissed
is called.为了使
Dismissible
工作,您必须做的是在调用onDismissed
时同步调用 State 中的State
_items.remove(item)
。
So, do not await
in between and do not simply remove the item from the repository, remove it from the actual State
too (I am specifically telling you this because I can see you are doing await shopListRepository.remove(event.product)
).因此,不要在两者之间
await
,也不要简单地从存储库中删除项目,也要从实际的State
中删除它(我特别告诉你这一点,因为我可以看到你正在做await shopListRepository.remove(event.product)
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.