【问题标题】:A dismissed Dismissible widget is still part of the tree. Getting this error repeatedly after removing item from list已关闭的 Dismissible 小部件仍然是树的一部分。从列表中删除项目后重复出现此错误
【发布时间】:2020-06-26 15:58:58
【问题描述】:

我已将 UniquiId 设置为 Dismissible Widget 的键。之后添加一些项目,我有些解雇了一个项目。当我转到下一页时,我收到一个错误(已关闭的 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'),
          ),
        ),
      ),
    );
  }
}

请帮我解决这个问题,尝试了很多仍然得到同样的错误。提前致谢。

【问题讨论】:

    标签: android flutter provider dismissible


    【解决方案1】:

    确保项目已从根列表中删除。如果您正在使用提供程序,则通过将 listView.builder 小部件保留在 Consumer 下来获取更新的数据。这样当您关闭一个项目时,它就会被更新。

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 1970-01-01
      • 2022-01-23
      • 2021-11-30
      • 2020-02-16
      • 1970-01-01
      • 2013-11-04
      • 2020-09-10
      • 2011-07-04
      相关资源
      最近更新 更多