简体   繁体   中英

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. 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.

    @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. So that when you dismiss an item it will be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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