0
 ElevatedButton(
    style: ElevatedButton.styleFrom(
    shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10))),
        onPressed: () {
            cubit.addItemToCart(
                id: cubit.getCartModel!.data[index].id);
        },
        child: Text(LocaleKeys.addToCart.tr()),
 )

I'm trying to add item to cart so this RangeError Appears to me if anyone know how to solve it, I'll Aprreciate his Effort

jraufeisen
  • 3,005
  • 7
  • 27
  • 43
Khaled
  • 15
  • 3

2 Answers2

0

Your range error comes from this line

cubit.getCartModel!.data[index]

It tells you that index is

  • larger than or equal to the number of elements in your list cubit.getCartModel
  • or smaller than zero

This is a problem because you can only select list elements in a range from 0...length - 1.

jraufeisen
  • 3,005
  • 7
  • 27
  • 43
0

Maybe this list is empty.

cubit.getCartModel!.data

and you are accessing it with

index=4

Vu Thanh
  • 319
  • 1
  • 14
  • API return 6 items in the list but first item only is the one which I can Add To cart the Other items return RangeError – Khaled Nov 17 '22 at 09:29