这是我的JSON数据结构:
[
[
{
"nos": 0,
"name": "A S MUSIC AND DANCE A CULTURAL ORGANIZATION",
"unique_id": "AN/2020/0259067",
"reg_details": [
{
"registered_with": "Registrar of Societies"
},
{
"type_of_ngo": "Registered Societies (Non-Government)"
},
{
"registration_no": "1534"
},
{
"copy_of_registration_certificate": "Available"
},
{
"copy_of_pan_card": "Available"
},
{
"act_name": "Registration Act,1860"
},
{
"city_of_registration": "Port Blair"
},
{
"state_of_registration": "ANDAMAN & NICOBAR ISLANDS"
},
{
"date_of_registration": "25-05-2016"
}
],
在我的this函数中:
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[0].regDetails[1].type_of_ngo);
}
当我运行这行代码时:
debugPrint(list[0].regDetails[1].type_of_ngo);
结果是好的
I/flutter ( 5579): List<dynamic>
I/chatty ( 5579): uid=10326(com.skillzup.mn.mn) 1.ui identical 117 lines
I/flutter ( 5579): List<dynamic>
I/flutter ( 5579): Registered Societies (Non-Government)
但当我运行这段代码时:
debugPrint(list[0].regDetails[2].registration_no);
我得到了这个错误:
I/flutter ( 5579): List<dynamic>
I/chatty ( 5579): uid=10326(com.skillzup.mn.mn) 1.ui identical 117 lines
I/flutter ( 5579): List<dynamic>
I/flutter ( 5579): null
这个也是一样的:
debugPrint(list[0].regDetails[3].copy_of_registration_certificate);
错误:
Unhandled Exception: NoSuchMethodError: Class 'RegDetails' has no instance getter 'copy_of_registration_certificate'.
我想从这个接口获得的最重要的数据是: debugPrint(list2.regDetails8.date_of_registration);
但是我得到了这个错误:
I/flutter ( 5579): List<dynamic>
I/chatty ( 5579): uid=10326(com.skillzup.mn.mn) 1.ui identical 117 lines
I/flutter ( 5579): List<dynamic>
I/flutter ( 5579): null
对于某些字段,数据显示正确,但对于某些字段,要么显示为空,要么显示某些错误。我遗漏了什么?
这是我的NGO和注册类:
class NGO {
String name;
String id;
List<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) {
var list = json['reg_details'] as List;
print(list.runtimeType);
List<RegDetails> regDetails = list.map((i) => RegDetails.fromJson(i)).toList();
return NGO(
id: json["unique_id"],
name: json['name'],
regDetails: regDetails
);
}
}
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['registeration_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'],
);
}
}
发布于 2021-04-14 18:18:30
由于这段代码中的拼写错误
factory RegDetails.fromJson(Map<String, dynamic> json){
return RegDetails(
registration_no: json['registration_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'],
);
}
}
https://stackoverflow.com/questions/67096137
复制相似问题