【发布时间】:2021-11-07 11:18:15
【问题描述】:
我正在将我的 API 数据收集到一个数组列表 List<GetTestFeeMap> reponseArray =[]; // Storing API response
并在每次调用 API 时向数组中添加新数据,然后使用 map 函数构建动态表。
现在我正在尝试计算列的总和并显示它(不一定在表格中)。
希望你能理解我的问题。
Future<void> GetTestFee() async {
var jsonResponse;
if (encTestId.isNotEmpty) {
var response = await http.post(
Uri.parse("http://gtyuigetvytun/api/medboapi/GetTestFee"),
body: ({
'EncPartnerId': encLabId,
'EncTestId': encTestId,
}));
if (response.statusCode == 200) {
print("Correct");
print(response.body);
jsonResponse = json.decode(response.body.toString());
print(jsonResponse);
getTestFeeObj=GetTestFeeMap.fromJson(jsonResponse);
setState(() {
reponseArray.add(getTestFeeObj!); // Adding data to my Arraylist
for(GetTestFeeMap elem in reponseArray){
feeSum += elem.fee as int;
discountSum += elem.discountedFee as int; // Doing calculation here
bookingSum += elem.bookingFee as int;
}
print(feeSum);
print(discountSum);
print(discountSum);
});
}
然后使用 ArrayList of data 创建我的动态表
DataTable(
columnSpacing: 13.0,
columns: <DataColumn>[
DataColumn(label: Text("Fee")),
DataColumn(label: Text("Discounted Fee")),
DataColumn(label: Text("Booking Fee")),
],
rows:reponseArray.map((testRowData){
return DataRow(
cells: [
DataCell(Text(testRowData.fee ?? '')),
DataCell(Text(testRowData.discountedFee ?? '')),
DataCell(Text(testRowData.bookingFee ?? ''))
]
);
}).toList()
),
【问题讨论】:
-
什么是你不知道的?你这样计算总和: int sum = 0; for (...) { sum += value;}