【发布时间】:2021-02-11 13:06:30
【问题描述】:
我正在尝试将 JSON 数组解析为我的应用程序中的列表,它与其他模型类一起使用,但我只收到了这个特定的错误。我在其他情况下使用了类似的代码,但它在这里不起作用。如果您只使用 List 而不是将类型更改为模型类并启动响应,它会起作用。
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
Class 'StudyList' has no instance method '[]'.
Receiver: Instance of 'StudyList'
Tried calling: []("reportStatus")
════════════════════════════════════════════════════════════════════════════════════════════════════
这是我的模型类:
class StudyList {
String studyUID;
String patientId;
String patientName;
String studyDateTime;
String modalityName;
String study;
String studyDescription;
String refferringPhysician;
String clinicalHistory;
String siteName;
String reportPath;
String attachmentPath;
String imageReceviedTime;
var reportReceviedTime;
int noOfImages;
String reportStatus;
String radiologistName;
String gender;
String age;
int series;
int siteID;
int radiologistID;
var reportReceviedTime1;
String radiologistAssignedTime;
int radcentreId;
String examdate;
int emergencyst;
String studycode;
var repdown;
String thumbnail;
int modality;
int status;
var studyview;
var studydown;
StudyList(
{this.studyUID,
this.patientId,
this.patientName,
this.studyDateTime,
this.modalityName,
this.study,
this.studyDescription,
this.refferringPhysician,
this.clinicalHistory,
this.siteName,
this.reportPath,
this.attachmentPath,
this.imageReceviedTime,
this.reportReceviedTime,
this.noOfImages,
this.reportStatus,
this.radiologistName,
this.gender,
this.age,
this.series,
this.siteID,
this.radiologistID,
this.reportReceviedTime1,
this.radiologistAssignedTime,
this.radcentreId,
this.examdate,
this.emergencyst,
this.studycode,
this.repdown,
this.thumbnail,
this.modality,
this.status,
this.studyview,
this.studydown});
StudyList.fromJson(Map<String, dynamic> json) {
studyUID = json['studyUID'];
patientId = json['patientId'];
patientName = json['patientName'];
studyDateTime = json['studyDateTime'];
modalityName = json['modalityName'];
study = json['study'];
studyDescription = json['studyDescription'];
refferringPhysician = json['refferringPhysician'];
clinicalHistory = json['clinicalHistory'];
siteName = json['siteName'];
reportPath = json['reportPath'];
attachmentPath = json['attachmentPath'];
imageReceviedTime = json['imageReceviedTime'];
reportReceviedTime = json['reportReceviedTime'];
noOfImages = json['noOfImages'];
reportStatus = json['reportStatus'];
radiologistName = json['radiologistName'];
gender = json['gender'];
age = json['age'];
series = json['series'];
siteID = json['siteID'];
radiologistID = json['radiologistID'];
reportReceviedTime1 = json['reportReceviedTime1'];
radiologistAssignedTime = json['radiologistAssignedTime'];
radcentreId = json['radcentreId'];
examdate = json['examdate'];
emergencyst = json['emergencyst'];
studycode = json['studycode'];
repdown = json['repdown'];
thumbnail = json['thumbnail'];
modality = json['modality'];
status = json['status'];
studyview = json['studyview'];
studydown = json['studydown'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['studyUID'] = this.studyUID;
data['patientId'] = this.patientId;
data['patientName'] = this.patientName;
data['studyDateTime'] = this.studyDateTime;
data['modalityName'] = this.modalityName;
data['study'] = this.study;
data['studyDescription'] = this.studyDescription;
data['refferringPhysician'] = this.refferringPhysician;
data['clinicalHistory'] = this.clinicalHistory;
data['siteName'] = this.siteName;
data['reportPath'] = this.reportPath;
data['attachmentPath'] = this.attachmentPath;
data['imageReceviedTime'] = this.imageReceviedTime;
data['reportReceviedTime'] = this.reportReceviedTime;
data['noOfImages'] = this.noOfImages;
data['reportStatus'] = this.reportStatus;
data['radiologistName'] = this.radiologistName;
data['gender'] = this.gender;
data['age'] = this.age;
data['series'] = this.series;
data['siteID'] = this.siteID;
data['radiologistID'] = this.radiologistID;
data['reportReceviedTime1'] = this.reportReceviedTime1;
data['radiologistAssignedTime'] = this.radiologistAssignedTime;
data['radcentreId'] = this.radcentreId;
data['examdate'] = this.examdate;
data['emergencyst'] = this.emergencyst;
data['studycode'] = this.studycode;
data['repdown'] = this.repdown;
data['thumbnail'] = this.thumbnail;
data['modality'] = this.modality;
data['status'] = this.status;
data['studyview'] = this.studyview;
data['studydown'] = this.studydown;
return data;
}
}
这是我从 API 解析数据的代码:
List<StudyList> _patientDataapi = List();
setState(() {
_shimmerLoader = true;
_patientDataapi = (json.decode(res.body))
.map<StudyList>((item) => StudyList.fromJson(item))
.toList();
});
最后,这是在制作 API 时获取的 JSON 数据:
[
{
"studyUID": "1.2.840.113619.2.55.3.2831168001.314.1602949790.991",
"patientId": "4789",
"patientName": "MR.ANIKET ADKAKE",
"studyDateTime": "2020-10-17T22:22:04",
"modalityName": "CT",
"study": "NECK PLAIN",
"studyDescription": "CT-NECK",
"refferringPhysician": "G.H.S",
"clinicalHistory": "H/O- LOSS OF VOICE",
"siteName": "Aditya sangli",
"reportPath": "",
"attachmentPath": "",
"imageReceviedTime": "2020-10-17T22:24:49",
"reportReceviedTime": null,
"noOfImages": 215,
"reportStatus": "Pending",
"radiologistName": "Dr.Agarwal",
"gender": "M",
"age": "026Y",
"series": 5,
"siteID": 209,
"radiologistID": 518,
"reportReceviedTime1": null,
"radiologistAssignedTime": "2020-10-17T22:48:00",
"radcentreId": 6,
"examdate": "2020-10-17T21:38:57",
"emergencyst": 0,
"studycode": "CT-21",
"repdown": null,
"thumbnail": "",
"modality": 1,
"status": 3,
"studyview": null,
"studydown": null
},
{
"studyUID": "1.2.840.113619.2.55.3.2831168001.314.1602949791.126",
"patientId": "CT-4792",
"patientName": "MR.ANIKET ADAKE",
"studyDateTime": "2020-10-17T23:49:32",
"modalityName": "CT",
"study": "NECK PLAIN",
"studyDescription": "CT-NECK PLAIN",
"refferringPhysician": "G.H.S",
"clinicalHistory": "H/O- LOSS OF VOICE",
"siteName": "Aditya sangli",
"reportPath": "C:\\KINSOLUTIONS\\Team\\Report\\1.2.840.113619.2.55.3.2831168001.314.1602949791.126.docx",
"attachmentPath": "",
"imageReceviedTime": "2020-10-17T23:50:29",
"reportReceviedTime": null,
"noOfImages": 273,
"reportStatus": "Final",
"radiologistName": "Dr.Agarwal",
"gender": "M",
"age": "025Y",
"series": 4,
"siteID": 209,
"radiologistID": 518,
"reportReceviedTime1": "2020-10-18T00:25:07",
"radiologistAssignedTime": "2020-10-17T23:52:08",
"radcentreId": 6,
"examdate": "2020-10-17T23:46:03",
"emergencyst": 0,
"studycode": "CT-21",
"repdown": true,
"thumbnail": "",
"modality": 1,
"status": 5,
"studyview": null,
"studydown": null
}
]
【问题讨论】:
标签: json flutter dart model jsonparser