Android json解码颤振中返回null的列表
以下是postman中的实际api响应{ “地位”:200, “电文”:“, “数据”:{ “用户详细信息”:{ “用户名”:“richu”, “电子邮件”:test96@gmail.com", “id”:“1” }, “员额”:[ { “id”:“1”, “用户id”:“1”, “post”:“post 1--你好”, “imagepath”:“uploads/posts/1.png”, “发布日期”:“2020-11-07 09:10:07”, “状态”:“0” }, { “id”:“2”, “用户id”:“1”, “post”:“post 2--你好”, “imagepath”:“uploads/posts/2.png”, “发布日期”:“2020-11-07 10:10:07”, “状态”:“0” }, { “id”:“3”, “用户id”:“1”, “post”:“post 3--SFDSFSDFSVBCVB”, “imagepath”:“uploads/posts/3.png”, “发布日期”:“2020-11-07 11:10:07”, “状态”:“0” } ], “追随者”:“5”, 以下内容:“0” } }我正在计算我需要一个帖子列表,但它只返回{“状态”:200,“消息”:“数据”:{“用户详细信息”:{“用户名”:“管理员”,“电子邮件”:test96@gmail.com“,“id”:“1”},“posts”:null,“followers”:“9”,“following”:“0”}}显示错误Android json解码颤振中返回null的列表,android,json,flutter,dart,jsondecoder,Android,Json,Flutter,Dart,Jsondecoder,以下是postman中的实际api响应{ “地位”:200, “电文”:“, “数据”:{ “用户详细信息”:{ “用户名”:“richu”, “电子邮件”:test96@gmail.com", “id”:“1” }, “员额”:[ { “id”:“1”, “用户id”:“1”, “post”:“post 1--你好”, “imagepath”:“uploads/posts/1.png”, “发布日期”:“2020-11-07 09:10:07”, “状态”:“0” }, { “id”:“2”,
E/flatter(12419):[ERROR:flatter/lib/ui/ui\u dart\u state.cc(166)]未处理的异常:NoSuchMethodError:对null调用了方法“map”。E/flatter(12419):接收者:空E/flatter(12419):尝试调用:map(Closure:(dynamic)=>Posts)E/flatter(12419):#0 Object.noSuchMethod(dart:core patch/Object_patch.dart:51:5)E/flatter(12419):#1 new Data.fromJson(包:campagain\u mobile/src/models/api\u models/user\u profile\u 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"],
);
Map<String, 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"],
);
Map<String, 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"],
);
Map<String, dynamic> toJson() => {
"username": username,
"email": email,
"id": id,
};
}
`
`
导入“dart:convert”;
进口“包装:颤振/基础.dart”;
UserProfileResponse userProfileResponseFromJson(字符串str)=>
UserProfileResponse.fromJson(json.decode(str));
字符串userProfileResponseToJson(UserProfileResponse数据)=>
encode(data.toJson());
类UserProfileResponse{
用户配置文件响应({
这个,身份,,
这条信息,
这个数据,,
});
最终国际地位;
最终字符串消息;
最终数据;
factory UserProfileResponse.fromJson(映射json)=>
用户配置文件响应(
状态:json[“状态”],
message:json[“message”],
data:data.fromJson(json[“data”]),
);
映射到JSON()=>{
“地位”:地位,
“信息”:信息,
“data”:data.toJson(),
};
}
类数据{
资料({
这是用户详细信息,
这个,帖子,,
这个,追随者,,
这,下面,,
});
最终用户详细信息用户详细信息;
最后员额名单;
最后的字符串跟随者;
最后的字符串跟随;
工厂数据.fromJson(映射json)=>数据(
userDetails:userDetails.fromJson(json[“userDetails”]),
posts:List.from(json[“posts”].map((x)=>posts.fromJson(x)))
,
followers:json[“followers”],
following:json[“following”],
);
映射到JSON()=>{
“userDetails”:userDetails.toJson(),
“posts”:List.from(posts.map((x)=>x.toJson()),
“追随者”:追随者,
“跟随”:跟随,
};
}
班级职务{
职位({
这个身份证,
这个.userId,
这个帖子,
这个.imagepath,
这个是postDate,
这个,身份,,
});
最终字符串id;
最终字符串用户标识;
最后一根弦柱;
最终字符串图像路径;
最终日期时间后日期;
最终字符串状态;
factory Posts.fromJson(映射json)=>Posts(
id:json[“id”],
userId:json[“user_id”],
post:json[“post”],
imagepath:json[“imagepath”],
postDate:DateTime.parse(json[“post_date”]),
状态:json[“状态”],
);
映射到JSON()=>{
“id”:id,
“用户id”:用户id,
“岗位”:岗位,
“imagepath”:imagepath,
“post_date”:postDate.toiso8601字符串(),
“地位”:地位,
};
}
类用户详细信息{
用户详细信息({
这个.username,
这是一封电子邮件,
这个身份证,
});
字符串用户名;
字符串电子邮件;
字符串id;
工厂UserDetails.fromJson(映射json)=>UserDetails(
用户名:json[“用户名”],
电子邮件:json[“电子邮件”],
id:json[“id”],
);
映射到JSON()=>{
“用户名”:用户名,
“电子邮件”:电子邮件,
“id”:id,
};
}
`
您需要将空安全性添加到JSON密钥,请尝试以下操作:
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> toJson() {
final Map<String, dynamic> data = new 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> toJson() {
final Map<String, dynamic> data = new 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> toJson() {
final Map<String, dynamic> data = new 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;
}
}
class UserProfileResponse{
智力状态;
字符串消息;
数据;
UserProfileResponse({this.status,this.message,this.data});
UserProfileResponse.fromJson(映射json){
status=json['status'];
message=json['message'];
data=json['data']!=null?新数据。fromJson(json['data']):null;
}
映射到JSON(){
最终地图数据=新地图();
数据['status']=this.status;
数据['message']=此.message;
如果(this.data!=null){
data['data']=this.data.toJson();
}
返回数据;
}
}
类数据{
用户详细信息用户详细信息;
列出员额;
弦乐追随者;
字符串跟踪;
数据({this.userDetails,this.posts,this.followers,this.following});
Data.fromJson(映射json){
userDetails=json['userDetails']!=null
?新的UserDetails.fromJson(json['UserDetails'])
:null;
if(json['posts']!=null){
posts=新列表();
json['posts'].forEach((v){
posts.add(new posts.fromJson(v));
});
}
followers=json['followers'];
following=json['following'];
}
映射到JSON(){
最终地图数据=新地图();
if(this.userDetails!=null){
data['userDetails']=this.userDetails.toJson();
}
if(this.posts!=null){
data['posts']=this.posts.map((v)=>v.toJson()).toList();
}
数据['followers']=this.followers;
数据['following']=this.following;
返回数据;
}
}
类用户详细信息{
字符串用户名;
字符串电子邮件;
字符串id;
UserDetails({this.username,this.email,this.id});
fromJson(映射json){
username=json['username'];
email=json['email'];
id=json['id'];
}
映射到JSON(){
最终地图数据=新地图();
数据['username']=this.username;
数据['email']=this.email;
数据['id']=this.id;
返回数据;
}
}
班级职务{
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.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) {
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.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) {
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"],
)
: null;
}
Map<String, 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) {
return json != null
? UserDetails(
username: json["username"],
email: json["email"],
id: json["id"],
)
: null;
}
Map<String, dynamic> toJson() => {
"username": username,
"email": email,
"id": id,
};
}