【问题标题】:How to calculate total Sum of all column cells in a table?如何计算表格中所有列单元格的总和?
【发布时间】: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;}

标签: json flutter api dart


【解决方案1】:

您应该检查您的 responseArray 项目并跟踪三个总和

int feeSum = 0;
int discountSum = 0;
int bookingSum = 0;

for(GetTestFeeMap elem in reponseArray){
  feeSum += elem.fee;
  discountSum += elem.discountedFee;
  bookingSum += elem.bookingFee;
}


【讨论】:

  • 我将for loop 放在我的API 函数setState(){} 中,现在只是尝试在控制台中打印以检查实现是否正确。不幸的是发生了异常Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
  • 由于reponseArray 项目是arrayList 项目,我相信它们是String 格式,这就是为什么它不计算总和?通过使String feeSum = "" 向我显示 Addition 作为 Sting 值 (500300) 类似这样的东西
  • 您可能应该通过将String? fee; 替换为int? fee; 来更改您的类定义如果您的API 返回字符串,您可以编辑您的fromJson 方法,将fee = json['Fee']; 更改为fee = int.parse(json['Fee']);
  • 知道了。但不是给800(例如:第一列),而是给我1300 .. 你的这个表达式在逻辑上也是正确的feeSum += elem.fee as int; 那么为什么它像(500+500+300)=1300 一样计算呢? [pastebin.com/tsj7Wkhc]
  • 第一个 api 调用==> 500 到我的 ArrayList ||第二个 Api 调用==> 300 到我的 ArrayList。从哪里得到额外的500
猜你喜欢
  • 2013-01-05
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多