Can';t将JSON数据打印到Dart对象中-“;'的实例;对象'&引用;
请帮忙! 把我当成一个颤抖的新手。我正在尝试将JSON数据转换为dart对象,然后将其保存到Firestore 我想了解NGO的详细信息,而不仅仅是实例。我该怎么办?我想我把所有的课程都做得很准确。我的函数中存在一些问题,此函数可能是:Can';t将JSON数据打印到Dart对象中-“;'的实例;对象'&引用;,json,flutter,dart,google-cloud-firestore,Json,Flutter,Dart,Google Cloud Firestore,请帮忙! 把我当成一个颤抖的新手。我正在尝试将JSON数据转换为dart对象,然后将其保存到Firestore 我想了解NGO的详细信息,而不仅仅是实例。我该怎么办?我想我把所有的课程都做得很准确。我的函数中存在一些问题,此函数可能是: Future<void> _NgoDetail() async { String jsonString = await _loadANgoAsset(); final jsonResponse = json.decode(jso
Future<void> _NgoDetail() async {
String jsonString = await _loadANgoAsset();
final jsonResponse = json.decode(jsonString);
var rest = jsonResponse[0] as List;
List list=[];
list= rest.map<NGO>((json) => NGO.fromJson(json)).toList();
debugPrint(list.toString());
}
然而:
这是我的NGO课程:
class NGO {
String name;
String id;
RegDetails regDetails;
contactDetails ContactDetails;
Members members;
WorkingSectors workingSectors;
NGO({this.members,this.regDetails,this.name,this.workingSectors,this.ContactDetails,this.id,});
factory NGO.fromjson(Map<String, dynamic> json){
return NGO(
id: json["id"],
name:json['name'],
regDetails: RegDetails.fromJson(json['regDetails']),
ContactDetails: contactDetails.fromJson(json['ContactDetails']),
members: Members.fromJson(json['members']),
workingSectors: WorkingSectors.fromJson(json['workingSectors'])
);
}
/* @required this.DateOfReg,
this.NGOType,
this.RegisteredWith,
this.FCRA,
this.PAN,
this.persons,
this.RegAddress,
this.RegDistrict,
this.RegState,
this.UniqueID,
this.WorkSectors});*/
}
以下是我在NGO中用于JSON解析的其他类:
class RegDetails{
String registration_no;
String type_of_ngo;
String registered_with;
String copy_of_pan_card;
String date_of_registration;
String city_of_registration;
String state_of_registration;
String FCRA;
RegDetails({this.FCRA='Not Available',this.city_of_registration,this.copy_of_pan_card,this.date_of_registration,this.registered_with,this.registration_no,this.state_of_registration,this.type_of_ngo});
factory RegDetails.fromJson(Map<String, dynamic> json){
return RegDetails(
registration_no: json['registered_no'],
type_of_ngo: json['type_of_ngo'],
registered_with: json['registered_with'],
copy_of_pan_card: json['copy_of_pan_card'],
date_of_registration: json['date_of_registeration'],
city_of_registration: json['city_of_registeration'],
state_of_registration: json['state_of_registeration'],
);
}
}
class contactDetails{
String address;
String city;
String state;
String telephone;
String mobile_no;
String website_url;
String e_mail;
contactDetails({this.address,this.city,this.e_mail,this.mobile_no,this.state,this.telephone,this.website_url});
factory contactDetails.fromJson(Map<String, dynamic> json){
return contactDetails(
address: json["address"],
city: json["city"],
state: json["state"],
telephone: json["telephone"],
mobile_no: json["mobile_no"],
website_url: json["website_url"],
e_mail: json["e_mail"]
);
}
}
class Members{
String name;
String designation;
String pan;
String adhaar;
Members({this.name,this.designation,this.pan,this.adhaar});
factory Members.fromJson(Map<String, dynamic> json) {
return Members(
name: json["name"],
designation: json["designation"],
pan:json["pan"],
adhaar:json["adhaar"]
);
}
}
class WorkingSectors{
String operational_area_states;
String key_issues;
String operational_area_district;
WorkingSectors({this.key_issues,this.operational_area_district,this.operational_area_states});
factory WorkingSectors.fromJson(Map<String, dynamic> json){
return WorkingSectors(
operational_area_states: json["operational_area_states"],
key_issues: json["key_issues"],
operational_area_district:json["operational_area_district"]
);
}
}
类注册表详细信息{
字符串注册号;
字符串类型\u的\u ngo;
已注册的字符串;
卡片的字符串副本;
字符串日期注册的日期;
字符串城市注册;
_注册的字符串状态_;
字符串FCRA;
RegDetails({this.FCRA='notavailable',this.city\u\u\u\u注册,this.copy\u\u\u\u-panu卡,this.date\u注册,this.registration\u-with,this.registration\u-no,this.state\u\u注册,this.type\u-of-ngo});
factory RegDetails.fromJson(映射json){
返回注册表详细信息(
注册号:json[“注册号”],
非政府组织的类型:json['type非政府组织的类型],
注册地址:json[“注册地址”],
卡的副本:json[“卡的副本”],
注册日期:json[“注册日期”],
注册城市:json[“注册城市”],
注册状态:json[“注册状态”],
);
}
}
班级联络资料{
字符串地址;
字符串城市;
字符串状态;
电话线;
字符串移动_no;
字符串网址;
字符串电子邮件;
联系人详细信息({this.address,this.city,this.e\u mail,this.mobile\u no,this.state,this.telephone,this.website\u url});
factory contactDetails.fromJson(映射json){
返回联系人详细信息(
地址:json[“地址”],
城市:json[“城市”],
状态:json[“状态”],
电话:json[“电话”],
手机号码:json[“手机号码”],
网站url:json[“网站url”],
电子邮件:json[“电子邮件”]
);
}
}
班级成员{
字符串名;
字符串名称;
串盘;
字符串adhaar;
成员({this.name,this.designation,this.pan,this.adhaar});
factory Members.fromJson(映射json){
返回成员(
名称:json[“名称”],
名称:json[“名称”],
pan:json[“pan”],
adhaar:json[“adhaar”]
);
}
}
阶级工作部门{
字符串操作区域状态;
字符串关键问题;
串操作区(区);;
工作区({这个.关键问题,这个.操作区,这个.操作区,这个州});
factory WorkingSectors.fromJson(映射json){
返回工作区(
操作区域状态:json[“操作区域状态”],
关键问题:json[“关键问题”],
运营区:json[“运营区”]
);
}
}
- 获得实例是一个好消息。这意味着您离访问对象所需的属性只有一步之遥
换衣服
为此:debugPrint(list.toString());
debugPrint(list[0].name); // This will print the name of the NGO at index[0] in your list.
- 当您说
时,它将打印该列表中的对象 例如,如果您的列表是print(List)
=>这将导致list _number=[1,2,3]编码>然后打印(_数字)
13
- 在您的情况下,
是一个list
,当您列表
时,您会得到现在看到的结果:debugPrint(list)
Instance of 'NGO', Instance of 'NGO', Instance of 'NGO', Instance of 'NGO', Instance of 'NGO'
Future<void> _NgoDetail() async {
String jsonString = await _loadANgoAsset();
final jsonResponse = json.decode(jsonString);
var rest = jsonResponse[0] as List;
List list=[];
list= rest.map<NGO>((json) => NGO.fromJson(json)).toList();
debugPrint(list.toString());
}
当你看到“对象”的实例时,不要惊慌,但要兴奋,你已经非常接近了 这工作正常
debugPrint(列表[0].name)代码>。但为什么不这样做:debugPrint(列表[0]。regDetails[8]。注册日期)
regDetails也是一个包含对象列表的实例,为什么无法打印?在regDetails
中是否有9个对象?你发布的内容的输出是什么regDetails
不是列表,但此debugPrint(列表[0].regDetails.date\u注册日期)代码>将起作用。此调试打印(列表[0].regDetails.date\u注册)代码>不起作用。它显示:未处理的异常:类型“List”不是类型“Map”的子类型。
。这意味着未正确解析该类型。以下是访问它的方式:debugPrint(列表[0]。regDetails[8][“注册日期”],因为ReaveDebug是一个映射,而不是Arror,您可以使用<代码>键<代码>而不是索引。尝试它并报告BACK,您不会在ReGebug类中返回它,您正在传递一张地图列表,而不是一张地图。这是与您原来的帖子不同的问题。请考虑将此标记为您的问题的答案,并计算。我们如何迭代地图列表,是为另一篇文章。
Instance of 'NGO', Instance of 'NGO', Instance of 'NGO', Instance of 'NGO', Instance of 'NGO'