flutterstateflutter-dependenciesshopping-cartstate-management

cart quantity not updated on screen in flutter


api calling and quantity updating successfully but on screen it is not showing updated quantity here is my code

here i assign value to qty in builder int qty = int.parse(cartProduct_list[0].products![index].quantity.toString());

quantity update

                              Row(
                                 children: [
                                   Text("Quantity : "),
                                   DropdownButton(
                                     isDense: true,
                                     hint: Text("Quantity : "),
                                     value: int.parse(cartProduct_list[0].products![index].quantity.toString()),
                                     onChanged: (value)  {
                                       setState(() {
                                         qty = value!;
                                       });
                                       print(qty);

                                       isload = true;
                                       _cartController.updateQty(email.toString(),
                                           sessionData.toString(),
                                           cartProduct_list[0].products![index].productId.toString(),
                                           qty!).then( (value) async {
                                             if(value == 200)
                                               {
                                               SharedPreferences prefs = await SharedPreferences.getInstance();
                                               sessionData = prefs.getString('sessionData');
                                               email = prefs.getString('username_controller');

                                               _cartController.getCart(email!, sessionData!).then((product) {
                                               setState(() {
                                               cartProduct_list.add(product);


                                               });
                                                 setState(() {
                                                   isload = false;

                                                 });
                                               }); }

                                           });
                                       ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                                           content: Text("Cart updated successfully.")));
                                     },
                                      items:  _createList()
                                   ),
                                 ],
                               ),

Solution

  • The update variable is incorrect. Please try this.

    onChange: (value) {
       setState(() {
          cartProduct_list[0].products![index].quantity = value!;
       });
    }