I have a ListView containing the observable list var cartItems = <CartItem>[].obs;
, when I tried to remove the last item in the ListView, I got error RangeError (index): Invalid value: Valid value range is empty: 0 and it was undone. Anyone can show me how to fix it? Thanks in advance. This is my code to hold the ListView:
return Obx(() {
if (_cartController.isLoading == false) {
if (_cartController.cartItems.length == 0) {
return Center(
child: Text("Your cart is empty."),
);
}
return Scaffold(
backgroundColor: Colors.white,
body: ListView.separated(
...
itemBuilder: (context, index) {
return Slidable(
endActionPane: ActionPane(
motion: ScrollMotion(),
children: [
SlidableAction(
onPressed: (context) {
setState(() {
_cartController.cartItems.removeAt(index);
});
_cartController
.removeFromCart(
userId: widget.user.id,
productId:
_cartController.cartItems[index].item.id)
.then((value) {...});
},
...
),
],
),
child: GestureDetector(
onTap: () => Get.to(() => ProductDetailsScreen(
product: _cartController.cartItems[index].item)),
child: _itemCard(
cartItem: _cartController.cartItems[index], index: index),
),
);
},
...
),
...
);
} else {
...
}
});