简体   繁体   English

已关闭的 Dismissible 小部件仍然是树的一部分。 从列表中删除项目后重复出现此错误

[英]A dismissed Dismissible widget is still part of the tree. Getting this error repeatedly after removing item from list

I have set UniquiId to the key of Dissmissible Widget.我已将 UniquiId 设置为 Dismissible Widget 的键。 Adding some items after that I'm some dismissing an item.之后添加一些项目,我有些解雇了一个项目。 When I am going to the next page I'm getting an error (A dismissed Dismissible widget is still part of the tree.) even after removing the item from the List using Provider in the dismissed callback.当我转到下一页时,我收到一个错误(已关闭的 Dismissible 小部件仍然是树的一部分。)即使在已关闭的回调中使用 Provider 从列表中删除项目后也是如此。

    @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: UniqueKey(),
      direction: DismissDirection.endToStart,
      confirmDismiss: (direction) {
        return showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text('Are you sure?'),
            content: Text(
              'Dou you want to remove the item from the cart?',
            ),
            actions: [
              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
                child: Text('No'),
              ),
              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
                child: Text('Yes'),
              ),
            ],
          ),
        );
      },
      onDismissed: (direction) {
          Provider.of<Cart>(context, listen: false)
              .removeItem(productId);
      },
      background: Container(
        color: Theme.of(context).errorColor,
        child: Icon(
          Icons.delete,
          color: Colors.white,
          size: 40,
        ),
        alignment: Alignment.centerRight,
        padding: EdgeInsets.only(right: 20),
        margin: EdgeInsets.symmetric(
          horizontal: 15,
          vertical: 4,
        ),
      ),
      child: Container(
        child: Padding(
          padding: EdgeInsets.all(8),
          child: ListTile(
            leading: CircleAvatar(
              child: Padding(
                padding: EdgeInsets.all(5),
                child: FittedBox(
                  child: Text('\₹${(price * quantity)}'),
                ),
              ),
            ),
            title: Text(title),
            subtitle: Text(laundryName.toUpperCase()),
            trailing: Text('$quantity x'),
          ),
        ),
      ),
    );
  }
}

Please help me out in this tried a lot still getting the same error.请帮助我在这个尝试了很多仍然得到同样的错误。 Thanks in advance.提前致谢。

Make sure the items are getting removed from the root List.确保项目已从根列表中删除。 If you are using provider, then get the updated data by keeping listView.builder widget under Consumer.如果您正在使用提供程序,则通过将 listView.builder 小部件保留在 Consumer 下来获取更新的数据。 So that when you dismiss an item it will be updated.这样当您关闭一个项目时,它将被更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM