This is my function in a Flutter app to calculate the final price of the product by multiplying the product price and product quantity from the list
int calci() {
int totalPrice = 0;
for (int i = 0; i < cartitem.length; i++) {
int price = int.parse(cartitem[i][1]);
int quantity = int.parse(cartitem[i][4]);
totalPrice += price * quantity;
}
return totalPrice;
}
and here is the code where I am displaying it
Text(
'₹${calci().toString()}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Now after running the app, this is giving me an error of typecasting I don’t know I tried a lot but was unable to resolve it.
Expected a value of type 'String', but got one of type 'int'
2
Answers
Int.parse needs a string to parse. The cart item is probably an integer. You can try
Make sure that your cartitem List does not have elements of dynamic types, like:
or
Declare cartitem as:
Although I would suggest using a list of objects of type item, something like:
example: