【发布时间】:2021-11-06 22:50:31
【问题描述】:
我正在尝试在我的表中动态添加 Row。但不是添加行,它只是更新我的行值。
Json 测试响应。
{
"EncPartnerId": "LEuT1eIlpLEMAAkZme3wpQ==",
"EncTestId": "U4exk+vfMGrn7cjNUa/PBw==",
"Fee": "100",
"DiscountedFee": "80",
"BookingFee": "50"
}
我的 API 函数按 Add 按钮我试图在我的表中添加测试名称和他们的数据
Future<void> GetTestFee() async {
var jsonResponse;
if (encTestId.isNotEmpty) {
var response = await http.post(
Uri.parse("http://myapitp.in/api/api/endpoint"),
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);
} else {
throw Exception("Faild to fetch");
}
} else {
throw Exception("Faild to fetch");
}
return GetTestFee();
}
然后将其打印为 DataTable。我认为这不是正确的方法。我也尝试在 build 方法中预先创建一个空列表,List<TableRow> tableRows = []; 但不知道如何在该列表中填充数据。
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columnSpacing: 13.0,
columns: <DataColumn>[
//DataColumn(label: Text("encPartnerId")),
//DataColumn(label: Text("encTestId")),
DataColumn(label: Text("TestName")),
DataColumn(label: Text("Fee")),
DataColumn(label: Text("Discounted Fee")),
DataColumn(label: Text("Booking Fee")),
],
rows: <DataRow>[
DataRow(
cells: <DataCell>[
// DataCell(Text(user?.encPartnerId ?? 'encPartnerId')),
// DataCell(Text(user?.encTestId ?? 'encPartnerId')),
DataCell(Container(child: Text(testName?? '',overflow: TextOverflow.ellipsis))),
DataCell(Text(getTestFeeObj?.fee ?? '')),
DataCell(Text(getTestFeeObj?.discountedFee ?? '')),
DataCell(Text(getTestFeeObj?.bookingFee ?? '')),
],
)
]
),
)
【问题讨论】:
-
您传递给
rows的数组当前被硬编码为单个DataRow。如果您希望它显示更多行,则需要以编程方式生成额外的行。 -
是的,我设法纠正了它。感谢 DevCommunity 中像你这样的人!我拿了一个空的 ArrayList[],每次调用我的 API 时都将项目添加到这个数组中,并且在表中我只是映射到这些项目。
-
@Abion47 您应该将答案作为答案发布,Toujo 应该将其标记为正确,这样其他人就会浪费时间尝试回答已经解决的问题
-
好的!我将在这里回答我自己的问题。