i am building an shopping and i am new to flutter i am calling data from my server by below method:
Future getCartData() async {
print("this is the token mytoken");
String url = 'https://myurl.com/apis/getCartItems';
http.Response response = await http.post(Uri.parse(url),
headers: {
'Authorization': "token mytoken",
"Content-Type": "application/json",
},
body: json.encode({
"username": "admin",
}));
print(response.body);
var data = json.decode(response.body);
print("cart data recieved :");
print(data.length);
return data;
}
and my json data looks like this:
{ {
"id":"8",
"title":"art-fusion gold",
"itemimage":"/media/10.jpg",
"price":3499.0
},
{
"id":"9",
"title":"ruby gold",
"itemimage":"/media/11.jpg",
"price":3699.0
}
}
and i am accessing every item details in future builder as:
list[index]['title']
everything working great i just want to add prices and store it into a variable so that i can show my users that what there total price is but i dont know how to ad this "price" variable from json.
can someone please help thanks in advance <3
CodePudding user response:
var totalPrice = 0;
and then in your loop
totalPrice = totalPrice list[index]['price'];
Then print or use this totalPrice variable.
But it is not a good way to access json data. Try to make data model/pojo class and then by using model/pojo class access data.
