简体   繁体   English

如何解决问题:使用 Bloc 时“已关闭的 Dismissible 小部件仍然是树的一部分”

[英]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.

相关问题 被关闭的 Dismissible 小部件仍然是树的一部分 - A dismissed Dismissible widget is still part of the tree Flutter:已关闭的可关闭小部件仍然是树的一部分 - Flutter: a dismissed dismissible widget is still part of the tree 已关闭的 Dismissible 小部件仍然是 flutter 中树的一部分 - A dismissed Dismissible widget is still part of the tree in flutter 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” 在 flutter - How to fix 'A dismissed Dismissible widget is still part of the tree.' in flutter 已关闭的 Dismissible 小部件仍然是树的一部分。 在 flutter - A dismissed Dismissible widget is still part of the tree. In flutter 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” flutter 错误 - How to fix "A dismissed Dismissible widget is still part of the tree." error in flutter 已关闭的 Dismissible 小部件仍然是树的一部分。 从列表中删除项目后重复出现此错误 - A dismissed Dismissible widget is still part of the tree. Getting this error repeatedly after removing item from list `一个被解雇的 Dismissible 小部件仍然是树的一部分`错误突然发生(FutureBuilder,ListView) - `A dismissed Dismissible widget is still part of the tree` error occurred suddenly (FutureBuilder, ListView) 如何在Flutter中的CustomScrollView中使用可禁用的小部件? - How to use a dismissible widget inside a CustomScrollView in Flutter? 将 pushNamed 与 Dismissible 小部件一起使用 - Use pushNamed with Dismissible Widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM