[英]How to resolve issue: “A dismissed Dismissible widget is still part of the tree” while use Bloc
在實現Dismissible
小部件時,我在刪除項目時出錯。
return Dismissible(
key: Key(widget.product.id),
onDismissed: (direction) {
setState(() {
BlocProvider.of<ManagerBloc>(context)
.add(RemoveProduct(widget.product));
});
這個孩子的父母看起來像
return ListView.builder(
itemCount: state.shopItem.length,
itemBuilder: (BuildContext context, int index) {
return ProductElement(product: state.shopItem[index]);
});
}
我將它移到父級並使用 blocProvider 刪除shopItem.removeAt()
但我仍然得到了這個問題。 即使我在列表中使用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());
}
我嘗試了 UniqueKey,結果是一樣的。 我的 product_id 是由 uuid.v1() 生成的 '64b7ff60-f782-11e9-a3e8-a9ee0aa87ea5'。
我認為問題在於您沒有刪除數據 model ,其列表在調用onDismissed
之后(同步)生成 UI。 假設您在小部件的State
中有一個List<Item> _items
,它存儲填充列表的 UI 數據 model。 為了使Dismissible
工作,您必須做的是在調用onDismissed
時同步調用 State 中的State
_items.remove(item)
。
因此,不要在兩者之間await
,也不要簡單地從存儲庫中刪除項目,也要從實際的State
中刪除它(我特別告訴你這一點,因為我可以看到你正在做await shopListRepository.remove(event.product)
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.