gpt4 book ai didi

json - Flutter错误类 'StudyList'没有实例方法 '[]'

转载 作者:行者123 更新时间:2023-12-03 03:48:29 24 4
gpt4 key购买 nike

我正在尝试将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:

[
{
"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的更新的模型类:
// To parse this JSON data, do
//
// final studyList = studyListFromJson(jsonString);

import 'dart:convert';

List<StudyList> studyListFromJson(String str) => List<StudyList>.from(json.decode(str).map((x) => StudyList.fromJson(x)));

String studyListToJson(List<StudyList> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class StudyList {
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,
});

String studyUid;
String patientId;
String patientName;
DateTime studyDateTime;
String modalityName;
String study;
String studyDescription;
String refferringPhysician;
String clinicalHistory;
String siteName;
String reportPath;
String attachmentPath;
DateTime imageReceviedTime;
dynamic reportReceviedTime;
int noOfImages;
String reportStatus;
String radiologistName;
String gender;
String age;
int series;
int siteId;
int radiologistId;
DateTime reportReceviedTime1;
DateTime radiologistAssignedTime;
int radcentreId;
DateTime examdate;
int emergencyst;
String studycode;
bool repdown;
String thumbnail;
int modality;
int status;
dynamic studyview;
dynamic studydown;

factory StudyList.fromJson(Map<String, dynamic> json) => StudyList(
studyUid: json["studyUID"],
patientId: json["patientId"],
patientName: json["patientName"],
studyDateTime: DateTime.parse(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: DateTime.parse(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"] == null ? null : DateTime.parse(json["reportReceviedTime1"]),
radiologistAssignedTime: DateTime.parse(json["radiologistAssignedTime"]),
radcentreId: json["radcentreId"],
examdate: DateTime.parse(json["examdate"]),
emergencyst: json["emergencyst"],
studycode: json["studycode"],
repdown: json["repdown"] == null ? null : json["repdown"],
thumbnail: json["thumbnail"],
modality: json["modality"],
status: json["status"],
studyview: json["studyview"],
studydown: json["studydown"],
);

Map<String, dynamic> toJson() => {
"studyUID": studyUid,
"patientId": patientId,
"patientName": patientName,
"studyDateTime": studyDateTime.toIso8601String(),
"modalityName": modalityName,
"study": study,
"studyDescription": studyDescription,
"refferringPhysician": refferringPhysician,
"clinicalHistory": clinicalHistory,
"siteName": siteName,
"reportPath": reportPath,
"attachmentPath": attachmentPath,
"imageReceviedTime": imageReceviedTime.toIso8601String(),
"reportReceviedTime": reportReceviedTime,
"noOfImages": noOfImages,
"reportStatus": reportStatus,
"radiologistName": radiologistName,
"gender": gender,
"age": age,
"series": series,
"siteID": siteId,
"radiologistID": radiologistId,
"reportReceviedTime1": reportReceviedTime1 == null ? null : reportReceviedTime1.toIso8601String(),
"radiologistAssignedTime": radiologistAssignedTime.toIso8601String(),
"radcentreId": radcentreId,
"examdate": examdate.toIso8601String(),
"emergencyst": emergencyst,
"studycode": studycode,
"repdown": repdown == null ? null : repdown,
"thumbnail": thumbnail,
"modality": modality,
"status": status,
"studyview": studyview,
"studydown": studydown,
};
}

这是用于以下方面的实现:
import 'package:flutter/material.dart';
import 'package:json_parsing_example/model2.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SampleApp(),
debugShowCheckedModeBanner: false,
);
}
}

class SampleApp extends StatefulWidget {
@override
_SampleAppState createState() => _SampleAppState();
}

class _SampleAppState extends State<SampleApp> {
bool _isLoading = false;
List<StudyList> list = List();

fetchData() async {
setState(() {
_isLoading = true;
});

String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");

// This is the above where you get the remote data
// Like var response = await http.get('your url');
// final studyList = studyListFromJson(data);

final studyList = studyListFromJson(data);

list = studyList;

setState(() {
_isLoading = false;
});
}

@override
void initState() {
super.initState();
fetchData();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your heading'),
),
body: Container(
child: _isLoading
? Center(child: CircularProgressIndicator())
: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
Text('${list[index].patientId }'),
Text('${list[index].patientName}')
],
),
);
})
],
)));
}
}

让我知道是否有效。

关于json - Flutter错误类 'StudyList'没有实例方法 '[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64585173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com