首页 > 解决方案 > Flutter 动态列表,Table 显示意外结果

问题描述

我正在尝试从 api 获取列表,将其保存为局部变量“myPastOrders”,当我打印它显示为 5 的长度时,api 创建者确认我没有办法接收更多,因为他以这种方式对其进行了编程。但是当我调用列表并在表上显示它时它显示 list.length*4 倍数据(但是当我打印 list.length 它显示 5),可能的原因是什么?我使用 list.clear() 清除了列表但仍然无法正常工作,缓存数据是否会发生这种情况,如果是这样,我该如何解决这个问题并仅显示 5 次表?

   List<PastOrder> myPastOrders;
   List<TableRow> pastOrderSummaryTableRow = [];
   myPastOrders = widget.user.pastOrders ?? widget.user.pastOrders;

  // int startIndex = 0;
  // int endIndex = 5;
  // List<String> subList = new List();
  // myPastOrders = myPastOrders.sublist(startIndex, endIndex);

  print(myPastOrders.length); // printing the length of list 5

  for (int i = 0; i < myPastOrders.length; i++) {
    print(myPastOrders[i]);
    pastOrderSummaryTableRow.add(
      TableRow(children: [
        Table(
            border: TableBorder.all(width: 1.0, color: Colors.black87),
            children: [
              TableRow(children: [
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      'Order No:',
                      style: TextStyle(
                          color: Colors.black87,
                          fontSize: 17.0,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      myPastOrders[i].orderNo,
                      style:
                          TextStyle(color: Colors.black87, fontSize: 17.0),
                    ),
                  ),
                ),
              ]),
              TableRow(children: [
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      'Vehicle:',
                      style: TextStyle(
                          color: Colors.black87,
                          fontSize: 17.0,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      myPastOrders[i].vehicleReg,
                      style:
                          TextStyle(color: Colors.black87, fontSize: 17.0),
                    ),
                  ),
                ),
              ]),
              TableRow(children: [
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      'Order Date:',
                      style: TextStyle(
                          color: Colors.black87,
                          fontSize: 17.0,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      myPastOrders[i].orderDate,
                      style:
                          TextStyle(color: Colors.black87, fontSize: 17.0),
                    ),
                  ),
                ),
              ]),
              TableRow(children: [
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      'Order Amount:',
                      style: TextStyle(
                          color: Colors.black87,
                          fontSize: 17.0,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                TableCell(
                  child: Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: Text(
                      myPastOrders[i].orderAmount,
                      style:
                          TextStyle(color: Colors.black87, fontSize: 17.0),
                    ),
                  ),
                ),
              ]),
              TableRow(
                  decoration: BoxDecoration(color: Colors.grey),
                  children: [
                    TableCell(
                      child: Padding(
                        padding: const EdgeInsets.all(6.0),
                        child: Text(
                          '',
                          style: TextStyle(
                              color: Colors.black87,
                              fontSize: 17.0,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                    TableCell(
                      child: Padding(
                        padding: const EdgeInsets.all(6.0),
                        child: Text(
                          "",
                          style: TextStyle(
                              color: Colors.black87, fontSize: 17.0),
                        ),
                      ),
                    ),
                  ]),
            ])
      ]),
    );
  }
  //
  // for (PastOrder pastOrderSummaryLast in myPastOrders) {
  //   // myPastOrders.clear();
  //   print("Past Order Length: ");
  //   print(myPastOrders.length);
  //   print(myPastOrders);
  //
  //   pastOrderSummaryTableRow.add(
  //     TableRow(children: [
  //       Table(
  //           border: TableBorder.all(width: 1.0, color: Colors.black87),
  //           children: [
  //             TableRow(children: [
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     'Order No:',
  //                     style: TextStyle(
  //                         color: Colors.black87,
  //                         fontSize: 17.0,
  //                         fontWeight: FontWeight.bold),
  //                   ),
  //                 ),
  //               ),
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     pastOrderSummaryLast.orderNo,
  //                     style:
  //                         TextStyle(color: Colors.black87, fontSize: 17.0),
  //                   ),
  //                 ),
  //               ),
  //             ]),
  //             TableRow(children: [
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     'Vehicle:',
  //                     style: TextStyle(
  //                         color: Colors.black87,
  //                         fontSize: 17.0,
  //                         fontWeight: FontWeight.bold),
  //                   ),
  //                 ),
  //               ),
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     pastOrderSummaryLast.vehicleReg,
  //                     style:
  //                         TextStyle(color: Colors.black87, fontSize: 17.0),
  //                   ),
  //                 ),
  //               ),
  //             ]),
  //             TableRow(children: [
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     'Order Date:',
  //                     style: TextStyle(
  //                         color: Colors.black87,
  //                         fontSize: 17.0,
  //                         fontWeight: FontWeight.bold),
  //                   ),
  //                 ),
  //               ),
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     pastOrderSummaryLast.orderDate,
  //                     style:
  //                         TextStyle(color: Colors.black87, fontSize: 17.0),
  //                   ),
  //                 ),
  //               ),
  //             ]),
  //             TableRow(children: [
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     'Order Amount:',
  //                     style: TextStyle(
  //                         color: Colors.black87,
  //                         fontSize: 17.0,
  //                         fontWeight: FontWeight.bold),
  //                   ),
  //                 ),
  //               ),
  //               TableCell(
  //                 child: Padding(
  //                   padding: const EdgeInsets.all(6.0),
  //                   child: Text(
  //                     pastOrderSummaryLast.orderAmount,
  //                     style:
  //                         TextStyle(color: Colors.black87, fontSize: 17.0),
  //                   ),
  //                 ),
  //               ),
  //             ]),
  //             TableRow(
  //                 decoration: BoxDecoration(color: Colors.grey),
  //                 children: [
  //                   TableCell(
  //                     child: Padding(
  //                       padding: const EdgeInsets.all(6.0),
  //                       child: Text(
  //                         '',
  //                         style: TextStyle(
  //                             color: Colors.black87,
  //                             fontSize: 17.0,
  //                             fontWeight: FontWeight.bold),
  //                       ),
  //                     ),
  //                   ),
  //                   TableCell(
  //                     child: Padding(
  //                       padding: const EdgeInsets.all(6.0),
  //                       child: Text(
  //                         "",
  //                         style: TextStyle(
  //                             color: Colors.black87, fontSize: 17.0),
  //                       ),
  //                     ),
  //                   ),
  //                 ]),
  //           ])
  //     ]),
  //   );
  // }

//在UI上显示表格

       Padding(
                    padding: const EdgeInsets.only(
                        right: 20, left: 20.0, top: 12),
                    child: Container(
                      color: Colors.grey.shade300,
                      child: Table(
                        border: TableBorder.all(
                            width: 1.0, color: Colors.black87),
                        children: pastOrderSummaryTableRow,
                      ),
                    ),
                  ),

标签: listflutteruitableviewdartcaching

解决方案


推荐阅读