list - 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,
),
),
),
解决方案
推荐阅读
- unit-testing - 如何模拟 Autofac IComponentContext.Resolve
- c# - 当玩家和敌人有一定距离并且敌人是预制件时,如何激活敌人?
- custom-component - 应用 VCL 样式时,根据属性在运行时更改按钮颜色
- javascript - How to populate object with findById? CastError: Cast to ObjectId failed for value
- java - 如何将 int [] 数组转换为 java.sql.Array
- mysql - 如何循环遍历 mysql 查询的结果?
- python - 使用 BeautifulSoup 4 从许多页面中获取多个元素
- eclipse - Maven 依赖项:无法解析导入,但存在于本地存储库中(Eclipse Oomph 解压缩设置任务)
- .net - Visual Studio Preview 19 中的 Xamarin IOS 说 IOS Designer 即将消失,但没有关于如何使用 Xcode 的链接
- yaml - 在 GitHub 操作中使用日期作为 ENV 变量