here is the actual api response in postman {
"status": 200,
"message": "",
"data": {
"userDetails": {
"username": "richu",
"email": "test96@gmail.com",
"id": "1"
},
"posts": [
{
"id": "1",
"user_id": "1",
"post": "post 1 -- hello",
"imagepath": "uploads/posts/1.png",
"post_date": "2020-11-07 09:10:07",
"status": "0"
},
{
"id": "2",
"user_id": "1",
"post": "post 2-- hello",
"imagepath": "uploads/posts/2.png",
"post_date": "2020-11-07 10:10:07",
"status": "0"
},
{
"id": "3",
"user_id": "1",
"post": "post 3-- sfdsfsdfsdfsdfsdfsdfsdfvbcvb",
"imagepath": "uploads/posts/3.png",
"post_date": "2020-11-07 11:10:07",
"status": "0"
}
],
"followers": "5",
"following": "0"
}
} programme am working out I need a list of posts but it returns only {"status":200,"message":"","data":{"userDetails":{"username":"Admin","email":"test96@gmail.com","id":"1"},"posts":null,"followers":"9","following":"0"}} shows error that
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)
well my code is as follows
`
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,
};
}
`