Home > Mobile >  Why i can not delete these dismissible widget from list
Why i can not delete these dismissible widget from list

Time:12-24

// ignore_for_file: prefer_const_constructors, unnecessary_brace_in_string_interps

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shopapp/providers/cart.dart';

class CartCheckoutItem extends StatelessWidget {
  final String id;
  final double price;
  final String productId;
  final int quantity;
  final String title;
  CartCheckoutItem({
    required this.id,
    required this.productId,
    required this.price,
    required this.quantity,
    required this.title,
  });
  @override
  Widget build(BuildContext context) {
    //Key zorunlu parametresi ile kaydırma animasyonu eklenmesini sağlar
    return Dismissible(
      key: ValueKey(id),
      background: Container(
        color: Theme.of(context).errorColor,
        child: Icon(
          Icons.delete,
          color: Colors.white,
          size: 40.0,
        ),
        alignment: Alignment.centerRight,
        padding: EdgeInsets.only(right: 30),
        margin: EdgeInsets.symmetric(
          horizontal: 15,
          vertical: 4,
        ),
      ),
      direction: DismissDirection.endToStart,
      onDismissed: (direction) {
        Provider.of<Cart>(context, listen: false).removeItem(productId);
      },
      child: Card(
        margin: EdgeInsets.symmetric(
          horizontal: 15,
          vertical: 4,
        ),
        child: ListTile(
          leading: CircleAvatar(
            backgroundColor: Theme.of(context).colorScheme.secondary,
            child: Container(
              margin: EdgeInsets.all(5),
              child: FittedBox(
                child: Text('\$${price}'),
              ),
            ),
          ),
          title: Text(title),
          subtitle: Text('Total: \$${price * quantity}'),
          trailing: Text('$quantity x'),
        ),
      ),
    );
  }
}

(A dismissed Dismissible widget is still part of the tree. Make sure to implement the onDismissed handler and to immediately remove the Dismissible widget from the application once that handler has fired. just want to delete item with dismissible from provider's list

CodePudding user response:

You can solve this error by just assigning UniqueKey to your widget rather than ValueKey.

Just make it from

Dismissible(
      key: ValueKey(id),

to

Dismissible(
      key: UniqueKey(),

UniqueKey forces a widget to build every time the widget renders.

CodePudding user response:

The issue is wrong use of provider. i added listen parameter in removeItem function's context and i found the answer. By the way that is a shop app code and unique key makes only crash in code

  • Related