0

I have error when widget ask product from cartData.

The getter 'product' was called on null.
Receiver: null
Tried calling: product

I don't know how to fix it. Shouldn't I use Product class in Cart class?

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  @override
  Widget build(BuildContext context){

    final cartData = Provider.of<CartDataProvider>(context);

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: ListView.builder(
        itemCount: cartData.cartItems.length,
        itemBuilder: (context, index) => Padding(
          padding: const EdgeInsets.symmetric(vertical: 10),
          child: Dismissible(
            key: Key(cartData.cartItems[index].product.id.toString()),
            direction: DismissDirection.endToStart,
            background: Container(
              padding: EdgeInsets.symmetric(horizontal: 20),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                color: Color(0xFFFFE6E6),
              ),
              child: Row(
                children: [
                  Spacer(),
                  Icon(Icons.delete_sweep_outlined),
                ],
              ),
            ),
            onDismissed: (direction) {
              setState(() {
                cartData.deleteItem(index);
              });
            },
            child: CartItemCard(cart: cartData.cartItems[index])
          ),
        )
      ),
    );
  }
}

This is my Cart model. I use Products like a list from another file

class Cart {
  final Product product;
  final int numOfitems;

  Cart({@required this.product, @required this.numOfitems});

}

class CartDataProvider with ChangeNotifier{
  Map<int, Cart> _cartItems = {
    1 : Cart(product: products[0], numOfitems: 2),
    2 : Cart(product: products[1], numOfitems: 1),
    3 : Cart(product: products[2], numOfitems: 3),
    4 : Cart(product: products[3], numOfitems: 2),
    5 : Cart(product: products[4], numOfitems: 1),
    6 : Cart(product: products[5], numOfitems: 4),
  };

  UnmodifiableMapView<int, Cart> get cartItems => 
    UnmodifiableMapView<int, Cart>(_cartItems);

 //Some methods

}
Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Chmelya
  • 57
  • 7
  • 1
    Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 23 '20 at 09:45

1 Answers1

0

Kindly remove the line below and see if it works:

key: Key(cartData.cartItems[index].product.id.toString()),

You can pass the index from the itembuilder as the key instead:

key: Key(index.toString()),
Sosu Alfred
  • 446
  • 3
  • 8
  • Yeah, sure, thaks you. but there ```child: CartItemCard(cart: cartData.cartItems[index])``` I can't do this – Chmelya Nov 23 '20 at 10:17
  • You're welcomed. The key can be any value that is unique to each element. Changing the value of the key does not affect: `child: CartItemCard(cart: cartData.cartItems[index])` – Sosu Alfred Nov 23 '20 at 10:35