I have to calculate the sub-total amount, shipping cost, and packaging cost and show them on CartView page.
I fetched data from API and I got a price, quantity, shipping cost, etc. There subtotal = price* quantity.
There is my code:
controller.obx((response) => response != null
? Container(
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: response.length,
itemBuilder: (BuildContext context, index) {
var subTotal =
response[index].quantity!.toDouble() *
response[index].product!.price!.toDouble();
return Column(
children: [
ListTile(
title: Text('Sub Total'.tr.toString()),
trailing: Text(subTotal.toString()),
),
ListTile(
title: Text('Discount'.tr.toString()),
trailing: Text(
'00'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
),
ListTile(
title: Text('Delivery Charge'.tr.toString()),
trailing: Text(
'${response[index].quantity!.toDouble() * response[index].product!.shippingCost!.toDouble()} tk'), /*${response[index].product!.price!.toInt()+response[index+1].product!.price!.toInt()}*/
),
ListTile(
title: Text('Packaging Cost'.tr.toString()),
trailing: Text( response[index].basketQty == '1'? '50': '120'),
),
],
);
},
),
)
: Text('')),
In this way I got subtotal for first item then got another subtotal for second item and so on, but I need just one subtitle that will have all item subtotal in sum form.
2
Answers
Use a
global variable
subTotal and keep adding the the total of each item in it.You can wrap your Container inside the Column and you can use fold method on your response (iterable) . Your code would look something like this