json - 如何将 TableRow 动态添加到 Table 中?
问题描述
我正在尝试在我的表中动态添加行。但不是添加行,它只是更新我的行值。
用于测试的 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 ?? '')),
],
)
]
),
)
解决方案
json响应
{
"EncPartnerId": "LEuT1eIlpryry",
"EncTestId": "U4exk+vfMyry",
"Fee": "100",
"DiscountedFee": "80",
"BookingFee": "50"
}
我的 JSON 响应模型类
class GetTestFeeMap { // Need this class
String? encPartnerId;
String? encTestId;
int? fee;
int? discountedFee;
int? bookingFee;
GetTestFeeMap(
{this.encPartnerId,
this.encTestId,
this.fee,
this.discountedFee,
this.bookingFee});
GetTestFeeMap.fromJson(Map<String, dynamic> json) {
encPartnerId = json['EncPartnerId'];
encTestId = json['EncTestId'];
//fee = json['Fee'];
fee = int.parse(json['Fee']);
// discountedFee = json['DiscountedFee'];
discountedFee = int.parse(json['DiscountedFee']);
//bookingFee = json['BookingFee'];
bookingFee = int.parse(json['BookingFee']);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['EncPartnerId'] = this.encPartnerId;
data['EncTestId'] = this.encTestId;
data['Fee'] = this.fee;
data['DiscountedFee'] = this.discountedFee;
data['BookingFee'] = this.bookingFee;
return data;
}
}
我在里面拿了一个空的 List[]
state
List<GetTestFeeMap> reponseArray = [];
// 存储 API 响应 || 稍后以表格形式显示考试费用获得 API 响应后将其添加到列表中
var response = await http.post( Uri.parse("http://ytyttiiiuiu.in/api/myapi/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); reponseModel.add(getTestFeeObj) // Here adding my response to list[]
然后将这些数据映射到我的 DataTable
child: 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!.toString())),
DataCell(Text(testRowData.discountedFee!.toString())),
DataCell(Text(testRowData.bookingFee!.toString()))
]);
}).toList()),
推荐阅读
- php - 在树枝模板 symfony 3 中显示流式响应
- wso2 - 与使用连接操作的 siddhi 查询的完全分布式部署相关的问题
- delphi - Delphi 7 - 是否可以在异常之外获取调用堆栈信息?
- c# - 输入数字时设置输入文本框的文化
- polymer - 聚合物 dom-如果不重新烧制
- hl7-fhir - 如何使用 Timing 表示事件偏移量?
- visual-studio - 更改 ReSharper 添加 using 语句的位置
- python - 'execfile' 未在 apache2 配置中定义
- ruby - 如何使用 nazrin gem 调用 AWS CloudSearch expr 方法?
- java - Java接口作为引用类型?