I am from a web background and am trying to learn flutter, I am building a cart and I will like to sum all the price based on what the user has in the cart, am using a PHP backend.
Below is my method that fetches the database
final String apiURL = 'mydomain/fetchcart.php';
Future<List<ProductData>> fetchcart() async {
var data = {'id': int.parse(id)};
var response = await http.post(apiURL, body: json.encode(data));
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<ProductData> studentList = items.map<ProductData>((json) {
return ProductData.fromJson(json);
}).toList();
return studentList;
} else {
throw Exception('Failed to load data from Server.');
}
}
Am using a futurebuilder to pass it to a widget, but I to want sum the item price
FutureBuilder<List<ProductData>>(
future: fetchcart(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, int index) {
return cartItems(
snapshot.data[index].pid,
snapshot.data[index].pName,
snapshot.data[index].pSellingPrice,
snapshot.data[index].pImage);
},
);
}),
question from:
https://stackoverflow.com/questions/65907850/sum-json-data-in-flutter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…