因此,我想创建一个循环小部件,其数据来自JSON格式的数据列表。
JSON格式如下所示:
{
"meta": {
"code": 200,
"status": "success",
"message": "Digital Cards successfully retrieved!"
},
"data": [
{
"indexs": "12",
"apply_date": "2021-03-14",
"id_member_card": "00",
"name_member_card": "Card 1",
"id_card": null,
"id_card_design": null,
"id_patner_card": null,
"start_date": "2021-01-01",
"end_date": "2050-12-31",
"periode_month": "12",
"min_trans_active": ".00",
"max_month_active": "12",
"min_trans_benefit": ".00",
"max_month_benefit": "1",
"allow_child_card": "1",
"max_transfer_point": ".00",
"expired_month": "12",
"expired_trx_amount": ".00",
"added_sp": "1",
"added_point": "1",
"flag_point": "0",
"flag_status": "1",
"recuser": "2071 ",
"recdate": null,
"recuserupd": "2071 ",
"recdateupd": "2021-01-07 14:35:31.000",
"image": null
},
{
"indexs": "4",
"apply_date": "2021-03-14",
"id_member_card": "01",
"name_member_card": "Card 2",
"id_card": "PC",
"id_card_design": "PC",
"id_patner_card": null,
"start_date": "2021-01-01",
"end_date": "2050-12-31",
"periode_month": "12",
"min_trans_active": ".00",
"max_month_active": "1",
"min_trans_benefit": ".00",
"max_month_benefit": "1",
"allow_child_card": "0",
"max_transfer_point": "10000.00",
"expired_month": "12",
"expired_trx_amount": "1.00",
"added_sp": "1",
"added_point": "1",
"flag_point": "1",
"flag_status": "1",
"recuser": "2071 ",
"recdate": "2017-02-10 14:53:51.000",
"recuserupd": "CS01 ",
"recdateupd": "2023-04-26 18:41:01.000",
"image": "~/Content/Images/CardDesigns/platinum_card.png",
"benefits": [
{
"indexs": "1",
"id_member_card": "01",
"name_benefit": "Voucher 25.000",
"description": "Free voucher 25.000 ready to use",
"recuser": "2071",
"recdate": "2023-07-20 09:00:00.000",
"recuserupd": null,
"recdateupd": null
},
{
"indexs": "7",
"id_member_card": "01",
"name_benefit": "Voucher 50.000",
"description": "Free voucher 50.000 ready to use",
"recuser": "2071",
"recdate": "2023-07-20 09:00:00.000",
"recuserupd": null,
"recdateupd": null
},
{
"indexs": "8",
"id_member_card": "01",
"name_benefit": "Voucher 100.000",
"description": "Free voucher 100.000 ready to use",
"recuser": "2071",
"recdate": "2023-07-20 09:00:00.000",
"recuserupd": null,
"recdateupd": null
}
]
}
]
}
字符串
我用JSON Converter做了一个模型,看起来像这样:
class DigitalCardsModel extends Equatable {
Meta? meta;
List<Data>? data;
DigitalCardsModel({this.meta, this.data});
DigitalCardsModel.fromJson(Map<String, dynamic> json) {
meta = json['meta'] != null ? Meta.fromJson(json['meta']) : null;
if (json['data'] != null) {
data = <Data>[];
json['data'].forEach((v) {
data!.add(Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
if (meta != null) {
data['meta'] = meta!.toJson();
}
if (this.data != null) {
data['data'] = this.data!.map((v) => v.toJson()).toList();
}
return data;
}
@override
List<Object?> get props => [data];
}
class Meta {
int? code;
String? status;
String? message;
Meta({this.code, this.status, this.message});
Meta.fromJson(Map<String, dynamic> json) {
code = json['code'];
status = json['status'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['code'] = code;
data['status'] = status;
data['message'] = message;
return data;
}
}
class Data {
String? indexs;
String? applyDate;
String? idMemberCard;
String? nameMemberCard;
String? idCard;
String? idCardDesign;
String? idPatnerCard;
String? startDate;
String? endDate;
String? periodeMonth;
String? minTransActive;
String? maxMonthActive;
String? minTransBenefit;
String? maxMonthBenefit;
String? allowChildCard;
String? maxTransferPoint;
String? expiredMonth;
String? expiredTrxAmount;
String? addedSp;
String? flagPoint;
String? flagStatus;
String? recuser;
String? recdate;
String? recuserupd;
String? recdateupd;
String? image;
List<Benefits>? benefits;
Data({
this.indexs,
this.applyDate,
this.idMemberCard,
this.nameMemberCard,
this.idCard,
this.idCardDesign,
this.idPatnerCard,
this.startDate,
this.endDate,
this.periodeMonth,
this.minTransActive,
this.maxMonthActive,
this.minTransBenefit,
this.maxMonthBenefit,
this.allowChildCard,
this.maxTransferPoint,
this.expiredMonth,
this.expiredTrxAmount,
this.addedSp,
this.flagPoint,
this.flagStatus,
this.recuser,
this.recdate,
this.recuserupd,
this.recdateupd,
this.image,
this.benefits,
});
Data.fromJson(Map<String, dynamic> json) {
indexs = json['indexs'];
applyDate = json['apply_date'];
idMemberCard = json['id_member_card'];
nameMemberCard = json['name_member_card'];
idCard = json['id_card'];
idCardDesign = json['id_card_design'];
idPatnerCard = json['id_patner_card'];
startDate = json['start_date'];
endDate = json['end_date'];
periodeMonth = json['periode_month'];
minTransActive = json['min_trans_active'];
maxMonthActive = json['max_month_active'];
minTransBenefit = json['min_trans_benefit'];
maxMonthBenefit = json['max_month_benefit'];
allowChildCard = json['allow_child_card'];
maxTransferPoint = json['max_transfer_point'];
expiredMonth = json['expired_month'];
expiredTrxAmount = json['expired_trx_amount'];
addedSp = json['added_sp'];
flagPoint = json['flag_point'];
flagStatus = json['flag_status'];
recuser = json['recuser'];
recdate = json['recdate'];
recuserupd = json['recuserupd'];
recdateupd = json['recdateupd'];
image = json['image'];
if (json['benefits'] != null) {
benefits = <Benefits>[];
json['benefits'].forEach((v) {
benefits!.add(Benefits.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['indexs'] = indexs;
data['apply_date'] = applyDate;
data['id_member_card'] = idMemberCard;
data['name_member_card'] = nameMemberCard;
data['id_card'] = idCard;
data['id_card_design'] = idCardDesign;
data['id_patner_card'] = idPatnerCard;
data['start_date'] = startDate;
data['end_date'] = endDate;
data['periode_month'] = periodeMonth;
data['min_trans_active'] = minTransActive;
data['max_month_active'] = maxMonthActive;
data['min_trans_benefit'] = minTransBenefit;
data['max_month_benefit'] = maxMonthBenefit;
data['allow_child_card'] = allowChildCard;
data['max_transfer_point'] = maxTransferPoint;
data['expired_month'] = expiredMonth;
data['expired_trx_amount'] = expiredTrxAmount;
data['added_sp'] = addedSp;
data['flag_point'] = flagPoint;
data['flag_status'] = flagStatus;
data['recuser'] = recuser;
data['recdate'] = recdate;
data['recuserupd'] = recuserupd;
data['recdateupd'] = recdateupd;
data['image'] = image;
if (benefits != null) {
data['benefits'] = benefits!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Benefits {
String? indexs;
String? idMemberCard;
String? nameBenefit;
String? description;
String? recuser;
String? recdate;
String? recuserupd;
String? recdateupd;
Benefits({
this.indexs,
this.idMemberCard,
this.nameBenefit,
this.description,
this.recuser,
this.recdate,
this.recuserupd,
this.recdateupd,
});
Benefits.fromJson(Map<String, dynamic> json) {
indexs = json['indexs'];
idMemberCard = json['id_member_card'];
nameBenefit = json['name_benefit'];
description = json['description'];
recuser = json['recuser'];
recdate = json['recdate'];
recuserupd = json['recuserupd'];
recdateupd = json['recdateupd'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['indexs'] = indexs;
data['id_member_card'] = idMemberCard;
data['name_benefit'] = nameBenefit;
data['description'] = description;
data['recuser'] = recuser;
data['recdate'] = recdate;
data['recuserupd'] = recuserupd;
data['recdateupd'] = recdateupd;
return data;
}
}
型
我已经设法通过使用如下的Map来访问"data"
:
AppBar(
leading: header(),
title: const Text('Digital Cards'),
bottom: TabBar(
tabs: state.data.data!.map(
(Data val) {
return Tab(
text: '${val.nameMemberCard}',
);
},
).toList(),
),
)
型
但是我需要访问JSON中所示的"data"
中的"benefits"
。
那么,如何访问"benefits"
呢?
我应该使用foreach
还是for
循环?
你能像我一样使用Map吗?
请帮忙,谢谢...
1条答案
按热度按时间92dk7w1h1#
获取你的JSON响应:
字符串
然后像这样使用它:
型