问题描述
这是邮递员 {
“状态”:200,
“信息”: ””,
“数据”:{
“ userDetails”:{
“ username”:“ richu”,
“电子邮件”:“ [email protected]”,
“ id”:“ 1”
},
“帖子”:[
{
“ id”:“ 1”,
“ user_id”:“ 1”,
“ post”:“第1条-您好”,
“ imagepath”:“ uploads / posts / 1.png”,
“ post_date”:“ 2020-11-07 09:10:07”,
“状态”:“ 0”
},
{
“ id”:“ 2”,
“ user_id”:“ 1”,
“ post”:“ post 2-- hello”,
“ imagepath”:“ uploads / posts / 2.png”,
“ post_date”:“ 2020-11-07 10:10:07”,
“状态”:“ 0”
},
{
“ id”:“ 3”,
“ user_id”:“ 1”,
“ post”:“第3个-sfdsfsdfsdfsdfsdfsdfsdfvbcvb”,
“ imagepath”:“ uploads / posts / 3.png”,
“ post_date”:“ 2020-11-07 11:10:07”,
“状态”:“ 0”
}
],
“关注者”:“ 5”,
“关注”:“ 0”
}
} 程序正在制定中,我需要一个帖子列表,但它仅返回 {“状态”:200,“消息”:“”,“数据”:{“ userDetails”:{“用户名”: “管理员”,“电子邮件”:“ [email protected]”,“ id”:“ 1”},“帖子”:null,“关注者”:“ 9”,“关注”:“ 0”}} 显示错误
E/Flutter (12419): [ERROR:Flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'map' was called on null. E/Flutter (12419): Receiver: null E/Flutter (12419): Tried calling: map(Closure: (dynamic) => Posts) E/Flutter (12419): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/Flutter (12419): #1 new Data.fromJson (package:campgain_mobile/src/models/api_models/user_profile_response.dart:55:47)
好,我的代码如下
`
import 'dart:convert';
import 'package:Flutter/foundation.dart';
UserProfileResponse userProfileResponseFromJson(String str) =>
UserProfileResponse.fromJson(json.decode(str));
String userProfileResponsetoJson(UserProfileResponse data) =>
json.encode(data.toJson());
class UserProfileResponse {
UserProfileResponse({
this.status,this.message,this.data,});
final int status;
final String message;
final Data data;
factory UserProfileResponse.fromJson(Map<String,dynamic> json) =>
UserProfileResponse(
status: json["status"],message: json["message"],data: Data.fromJson(json["data"]),);
Map<String,dynamic> toJson() => {
"status": status,"message": message,"data": data.toJson(),};
}
class Data {
Data({
this.userDetails,this.posts,this.followers,this.following,});
final UserDetails userDetails;
final List<Posts> posts;
final String followers;
final String following;
factory Data.fromJson(Map<String,dynamic> json) => Data(
userDetails: UserDetails.fromJson(json["userDetails"]),posts: List<Posts>.from(json["posts"].map((x) => Posts.fromJson(x))),followers: json["followers"],following: json["following"],dynamic> toJson() => {
"userDetails": userDetails.toJson(),"posts": List<dynamic>.from(posts.map((x) => x.toJson())),"followers": followers,"following": following,};
}
class Posts {
Posts({
this.id,this.userId,this.post,this.imagepath,this.postDate,this.status,});
final String id;
final String userId;
final String post;
final String imagepath;
final DateTime postDate;
final String status;
factory Posts.fromJson(Map<String,dynamic> json) => Posts(
id: json["id"],userId: json["user_id"],post: json["post"],imagepath: json["imagepath"],postDate: DateTime.parse(json["post_date"]),status: json["status"],dynamic> toJson() => {
"id": id,"user_id": userId,"post": post,"imagepath": imagepath,"post_date": postDate.toIso8601String(),"status": status,};
}
class UserDetails {
UserDetails({
this.username,this.email,this.id,});
String username;
String email;
String id;
factory UserDetails.fromJson(Map<String,dynamic> json) => UserDetails(
username: json["username"],email: json["email"],id: json["id"],dynamic> toJson() => {
"username": username,"email": email,"id": id,};
}
`
解决方法
您需要为JSON密钥添加null安全性,请尝试以下操作:
class UserProfileResponse {
int status;
String message;
Data data;
UserProfileResponse({this.status,this.message,this.data});
UserProfileResponse.fromJson(Map<String,dynamic> json) {
status = json['status'];
message = json['message'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}
Map<String,dynamic> toJson() {
final Map<String,dynamic> data = new Map<String,dynamic>();
data['status'] = this.status;
data['message'] = this.message;
if (this.data != null) {
data['data'] = this.data.toJson();
}
return data;
}
}
class Data {
UserDetails userDetails;
List<Posts> posts;
String followers;
String following;
Data({this.userDetails,this.posts,this.followers,this.following});
Data.fromJson(Map<String,dynamic> json) {
userDetails = json['userDetails'] != null
? new UserDetails.fromJson(json['userDetails'])
: null;
if (json['posts'] != null) {
posts = new List<Posts>();
json['posts'].forEach((v) {
posts.add(new Posts.fromJson(v));
});
}
followers = json['followers'];
following = json['following'];
}
Map<String,dynamic>();
if (this.userDetails != null) {
data['userDetails'] = this.userDetails.toJson();
}
if (this.posts != null) {
data['posts'] = this.posts.map((v) => v.toJson()).toList();
}
data['followers'] = this.followers;
data['following'] = this.following;
return data;
}
}
class UserDetails {
String username;
String email;
String id;
UserDetails({this.username,this.email,this.id});
UserDetails.fromJson(Map<String,dynamic> json) {
username = json['username'];
email = json['email'];
id = json['id'];
}
Map<String,dynamic>();
data['username'] = this.username;
data['email'] = this.email;
data['id'] = this.id;
return data;
}
}
class Posts {
String id;
String userId;
String post;
String imagepath;
String postDate;
String status;
Posts(
{this.id,this.userId,this.post,this.imagepath,this.postDate,this.status});
Posts.fromJson(Map<String,dynamic> json) {
id = json['id'];
userId = json['user_id'];
post = json['post'];
imagepath = json['imagepath'];
postDate = json['post_date'];
status = json['status'];
}
Map<String,dynamic>();
data['id'] = this.id;
data['user_id'] = this.userId;
data['post'] = this.post;
data['imagepath'] = this.imagepath;
data['post_date'] = this.postDate;
data['status'] = this.status;
return data;
}
}
,
当您尝试在json ['posts']上调用map方法时,发生错误。如果json ['posts']为null,则将发生null指针异常,因此您需要确保编码器和解码器为null安全。使用下面的代码
import 'dart:convert';
UserProfileResponse userProfileResponseFromJson(String str) =>
UserProfileResponse.fromJson(json.decode(str));
String userProfileResponseToJson(UserProfileResponse data) =>
json.encode(data.toJson());
class UserProfileResponse {
UserProfileResponse({
this.status,this.data,});
final int status;
final String message;
final Data data;
factory UserProfileResponse.fromJson(Map<String,dynamic> json) =>
UserProfileResponse(
status: json["status"],message: json["message"],data: Data.fromJson(json["data"]),);
Map<String,dynamic> toJson() => {
"status": status,"message": message,"data": data?.toJson(),};
}
class Data {
Data({
this.userDetails,this.following,});
final UserDetails userDetails;
final List<Posts> posts;
final String followers;
final String following;
factory Data.fromJson(Map<String,dynamic> json) {
return json != null
? Data(
userDetails: UserDetails.fromJson(json["userDetails"]),posts: json["posts"] != null
? List<Posts>.from(json["posts"].map((x) => Posts.fromJson(x)))
: null,followers: json["followers"],following: json["following"],)
: null;
}
Map<String,dynamic> toJson() => {
"userDetails": userDetails?.toJson(),"posts": posts != null
? List<dynamic>.from(posts?.map((x) => x.toJson()))
: null,"followers": followers,"following": following,};
}
class Posts {
Posts({
this.id,this.status,});
final String id;
final String userId;
final String post;
final String imagepath;
final DateTime postDate;
final String status;
factory Posts.fromJson(Map<String,dynamic> json) {
return json != null
? Posts(
id: json["id"],userId: json["user_id"],post: json["post"],imagepath: json["imagepath"],postDate: DateTime.parse(json["post_date"]),status: json["status"],dynamic> toJson() => {
"id": id,"user_id": userId,"post": post,"imagepath": imagepath,"post_date": postDate?.toIso8601String(),"status": status,};
}
class UserDetails {
UserDetails({
this.username,this.id,});
String username;
String email;
String id;
factory UserDetails.fromJson(Map<String,dynamic> json) {
return json != null
? UserDetails(
username: json["username"],email: json["email"],id: json["id"],dynamic> toJson() => {
"username": username,"email": email,"id": id,};
}