Flutter 如何解决方法';[]和#x27;在空颤振时被调用
我在尝试解析json对象时一直遇到这个错误。我的json对象中的某些字段在用户设置时默认为空。但我需要在应用程序启动后检索它们。但是,由于这些空值,我无法将数据解析到我的对象中Flutter 如何解决方法';[]和#x27;在空颤振时被调用,flutter,dart,Flutter,Dart,我在尝试解析json对象时一直遇到这个错误。我的json对象中的某些字段在用户设置时默认为空。但我需要在应用程序启动后检索它们。但是,由于这些空值,我无法将数据解析到我的对象中 : [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. E/flutter ( 9117): Receiver: null E/fl
: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 9117): Receiver: null
E/flutter ( 9117): Tried calling: []("degree")
我如何正确地解决这个问题。这是我的模型课
User.dart
class User {
final String first_name;
final String email;
final String last_name;
final String country;
final String gender;
final String phone;
final String degree;
final String linkedin;
final String institution;
final String profile_image;
final String created_at;
final String updated_at;
final String category;
final String industry;
final String bio_interest;
final String fav_quote;
final String isAdmin;
final String current_job;
final String company;
final String position;
final String state_of_origin;
int id = 0;
User(
this.first_name,
this.email,
this.last_name,
this.country,
this.gender,
this.phone,
this.degree,
this.institution,
this.profile_image,
this.created_at,
this.updated_at,
this.company,
this.isAdmin,
this.linkedin,
this.category,
this.industry,
this.bio_interest,
this.fav_quote,
this.position,
this.current_job,
this.state_of_origin,
this.id);
Map<String, dynamic> toJson() => {
'first_name': first_name,
'email': email,
'last_name': last_name,
'country': country,
'gender': gender,
'phone': phone,
'isAdmin': isAdmin,
'company': company,
'linkedin': linkedin,
'position': position,
'degree': degree,
'institution': institution,
'profile_image': profile_image,
'created_at': created_at,
'updated_at': updated_at,
'category': category,
'industry': industry,
'bio_interest': bio_interest,
'fav_quote': fav_quote,
'current_job': current_job,
'state_of_origin': state_of_origin,
'id': id,
};
User.fromJson(Map<String, dynamic> json)
: first_name = json['first_name'],
email = json['email'],
last_name = json['last_name'],
country = json['country'],
gender = json['gender'],
phone = json['phone'],
degree = json['education']['degree'],
linkedin = json['employment']['linkedin'],
institution = json['education']['institution'],
position = json['employment']['position'],
isAdmin = json['isAdmin'],
profile_image = json['profile_image'],
created_at = json['created_at'],
updated_at = json['updated_at'],
category = json['category'],
industry = json['industry'],
company = json['employment']['company'],
bio_interest = json['bio_interest'],
fav_quote = json['fav_quote'],
current_job = json['current_job'],
state_of_origin = json['state_of_origin'],
id = json['id'];
}
我意识到json数据中的一些字段是空的。e、 g优惠和就业。有没有一种方法我可以给他们默认值,这样我就可以绕过这个错误 在从列表访问数据之前添加空检查
class User {
final String first_name;
final String email;
final String last_name;
final String country;
final String gender;
final String phone;
final String degree;
final String linkedin;
final String institution;
final String profile_image;
final String created_at;
final String updated_at;
final String category;
final String industry;
final String bio_interest;
final String fav_quote;
final String isAdmin;
final String current_job;
final String company;
final String position;
final String state_of_origin;
int id = 0;
User(
this.first_name,
this.email,
this.last_name,
this.country,
this.gender,
this.phone,
this.degree,
this.institution,
this.profile_image,
this.created_at,
this.updated_at,
this.company,
this.isAdmin,
this.linkedin,
this.category,
this.industry,
this.bio_interest,
this.fav_quote,
this.position,
this.current_job,
this.state_of_origin,
this.id);
Map<String, dynamic> toJson() => {
'first_name': first_name,
'email': email,
'last_name': last_name,
'country': country,
'gender': gender,
'phone': phone,
'isAdmin': isAdmin,
'company': company,
'linkedin': linkedin,
'position': position,
'degree': degree,
'institution': institution,
'profile_image': profile_image,
'created_at': created_at,
'updated_at': updated_at,
'category': category,
'industry': industry,
'bio_interest': bio_interest,
'fav_quote': fav_quote,
'current_job': current_job,
'state_of_origin': state_of_origin,
'id': id,
};
User.fromJson(Map<String, dynamic> json)
: first_name = json['first_name'],
email = json['email'],
last_name = json['last_name'],
country = json['country'],
gender = json['gender'],
phone = json['phone'],
degree = getFromList(json['education'], 'degree'),
linkedin = getFromList(json['employment'], 'linkedin'),
institution = getFromList(json['education'], 'institution'),
position = getFromList(json['employment'], 'position'),
isAdmin = json['isAdmin'],
profile_image = json['profile_image'],
created_at = json['created_at'],
updated_at = json['updated_at'],
category = json['category'],
industry = json['industry'],
company = getFromList(json['employment'], 'company'),
bio_interest = json['bio_interest'],
fav_quote = json['fav_quote'],
current_job = json['current_job'],
state_of_origin = json['state_of_origin'],
id = json['id'];
}
String getFromList(Map<String, dynamic> json, String key) {
return json != null ? json[key] : "";
}
类用户{
最后一个字符串名;
最终字符串电子邮件;
最后一个字符串last_name;
最终字符串国家;
最终字符串性别;
最终字符串电话;
最终串度;
最后一个字符串linkedin;
最终字符串机构;
最终字符串配置文件\u图像;
在上创建的最终字符串;
最终字符串已更新至;
最终字符串类别;
最后一串产业;
最终字符串bio_兴趣;
最终字符串fav_quote;
最后一个字符串是admin;
当前作业的最终字符串;
最终字符串公司;
最终管柱位置;
_原点的最终字符串状态_;
int id=0;
使用者(
这个名字,
这是一封电子邮件,
这个姓,
这个国家,
这个,性别,
这个电话,
这个学位,
这个机构,,
这个.profile_图像,
这是在,
这个。更新于,
这家公司,
这位是艾德明,
这是linkedin,
这一类,,
这个行业,,
这是你的兴趣,
这是我的最爱,
这个位置,,
这是你目前的工作,
这个州的起源,
这是(身份证);
映射到JSON()=>{
“名字”:名字,
“电子邮件”:电子邮件,
“姓氏”:姓氏,
"国":国,,
"性别":性别,,
“电话”:电话,
“isAdmin”:isAdmin,
“公司”:公司,
“linkedin”:linkedin,
“位置”:位置,
“学位”:学位,
"制度":制度,,
“配置文件图像”:配置文件图像,
“创建时间”:创建时间,
“更新时间”:更新时间,
“类别”:类别,
"工业":工业,,
“生物兴趣”:生物兴趣,
“fav_quote”:fav_quote,
“当前作业”:当前作业,
“起源国”:起源国,
“id”:id,
};
User.fromJson(映射json)
:first_name=json['first_name'],
email=json['email'],
last_name=json['last_name'],
country=json['country'],
性别=json['gender'],
phone=json['phone'],
degree=getFromList(json['education'],'degree'),
linkedin=getFromList(json['employment'],'linkedin'),
institution=getFromList(json['education'],'institution'),
position=getFromList(json['employment'],'position'),
isAdmin=json['isAdmin'],
profile_image=json['profile_image'],
created_at=json['created_at'],
updated_at=json['updated_at'],
category=json['category'],
industry=json['industry'],
company=getFromList(json['employment','company'),
bio_interest=json['bio_interest'],
fav_quote=json['fav_quote'],
当前作业=json[“当前作业”],
原产地状态=json[“原产地状态”],
id=json['id'];
}
字符串getFromList(映射json,字符串键){
返回json!=null?json[key]:“”;
}
为什么投反对票?我也在想@night\u owl
class User {
final String first_name;
final String email;
final String last_name;
final String country;
final String gender;
final String phone;
final String degree;
final String linkedin;
final String institution;
final String profile_image;
final String created_at;
final String updated_at;
final String category;
final String industry;
final String bio_interest;
final String fav_quote;
final String isAdmin;
final String current_job;
final String company;
final String position;
final String state_of_origin;
int id = 0;
User(
this.first_name,
this.email,
this.last_name,
this.country,
this.gender,
this.phone,
this.degree,
this.institution,
this.profile_image,
this.created_at,
this.updated_at,
this.company,
this.isAdmin,
this.linkedin,
this.category,
this.industry,
this.bio_interest,
this.fav_quote,
this.position,
this.current_job,
this.state_of_origin,
this.id);
Map<String, dynamic> toJson() => {
'first_name': first_name,
'email': email,
'last_name': last_name,
'country': country,
'gender': gender,
'phone': phone,
'isAdmin': isAdmin,
'company': company,
'linkedin': linkedin,
'position': position,
'degree': degree,
'institution': institution,
'profile_image': profile_image,
'created_at': created_at,
'updated_at': updated_at,
'category': category,
'industry': industry,
'bio_interest': bio_interest,
'fav_quote': fav_quote,
'current_job': current_job,
'state_of_origin': state_of_origin,
'id': id,
};
User.fromJson(Map<String, dynamic> json)
: first_name = json['first_name'],
email = json['email'],
last_name = json['last_name'],
country = json['country'],
gender = json['gender'],
phone = json['phone'],
degree = getFromList(json['education'], 'degree'),
linkedin = getFromList(json['employment'], 'linkedin'),
institution = getFromList(json['education'], 'institution'),
position = getFromList(json['employment'], 'position'),
isAdmin = json['isAdmin'],
profile_image = json['profile_image'],
created_at = json['created_at'],
updated_at = json['updated_at'],
category = json['category'],
industry = json['industry'],
company = getFromList(json['employment'], 'company'),
bio_interest = json['bio_interest'],
fav_quote = json['fav_quote'],
current_job = json['current_job'],
state_of_origin = json['state_of_origin'],
id = json['id'];
}
String getFromList(Map<String, dynamic> json, String key) {
return json != null ? json[key] : "";
}