Я пытаюсь разобрать следующий JSON в dart, но всякий раз, когда я запускаю свой метод get, я ничего не получал в своем объекте userPosts. Я не знаю, что я здесь делаю не так. Я создал следующий класс модели, используя app.quicktype.io
Это JSON, который я пытаюсь проанализировать.
[
{
"_id": "5eedd6ee16bec833275bc929",
"caption": "Test",
"postImages": "",
"postVideos": "",
"postAudio": "",
"postSharedBy": [
""
],
"tag": [
{
"userId": "",
"name": "",
"guruzziId": ""
}
],
"location": "",
"locationLatLong": [
""
],
"anonymous": false,
"flagInappropriate": [
""
],
"likes": [
""
],
"userId": "5eedbd19996dc053453ff24c",
"createdAt": "2020-06-20 09:29:18",
"updatedAt": "2020-06-20 09:29:18",
"status": true,
"userDetail": {
"_id": "5eedbd19996dc053453ff24c",
"guruzziId": "_bxgza1vgw",
"firstName": "Archit Arora",
"lastName": "",
"photoUrl": ""
}
}
]
Это это созданный мной модальный
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
List<Post> postFromJson(String str) {
return List<Post>.from(jsonDecode(str).map((x) => Post.fromJson(x)));
}
String postToJson(List<Post> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Post {
Post({
this.id,
this.caption,
this.postImages,
this.postVideos,
this.postAudio,
this.postSharedBy,
this.tag,
this.location,
this.locationLatLong,
this.anonymous,
this.flagInappropriate,
this.likes,
this.userId,
this.createdAt,
this.updatedAt,
this.status,
this.userDetail,
});
String id;
String caption;
String postImages;
String postVideos;
String postAudio;
List<String> postSharedBy;
List<Tag> tag;
String location;
List<String> locationLatLong;
bool anonymous;
List<String> flagInappropriate;
List<String> likes;
String userId;
String createdAt;
String updatedAt;
bool status;
UserDetail userDetail;
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json["_id"],
caption: json["caption"],
postImages: json["postImages"],
postVideos: json["postVideos"],
postAudio: json["postAudio"],
postSharedBy: List<String>.from(json["postSharedBy"].map((x) => x)),
tag: List<Tag>.from(json["tag"].map((x) => Tag.fromJson(x))),
location: json["location"],
locationLatLong: List<String>.from(json["locationLatLong"].map((x) => x)),
anonymous: json["anonymous"],
flagInappropriate: List<String>.from(json["flagInappropriate"].map((x) => x)),
likes: List<String>.from(json["likes"].map((x) => x)),
userId: json["userId"],
createdAt: json["createdAt"],
updatedAt: json["updatedAt"],
status: json["status"],
userDetail: UserDetail.fromJson(json["userDetail"]),
);
}
Map<String, dynamic> toJson() => {
"_id": id,
"caption": caption,
"postImages": postImages,
"postVideos": postVideos,
"postAudio": postAudio,
"postSharedBy": List<dynamic>.from(postSharedBy.map((x) => x)),
"tag": List<dynamic>.from(tag.map((x) => x.toJson())),
"location": location,
"locationLatLong": List<dynamic>.from(locationLatLong.map((x) => x)),
"anonymous": anonymous,
"flagInappropriate":
List<dynamic>.from(flagInappropriate.map((x) => x)),
"likes": List<dynamic>.from(likes.map((x) => x)),
"userId": userId,
"createdAt": createdAt,
"updatedAt": updatedAt,
"status": status,
"userDetail": userDetail.toJson(),
};
}
class Tag {
Tag({
this.userId,
this.name,
this.guruzziId,
});
String userId;
String name;
String guruzziId;
factory Tag.fromJson(Map<String, dynamic> json) => Tag(
userId: json["userId"],
name: json["name"],
guruzziId: json["guruzziId"],
);
Map<String, dynamic> toJson() => {
"userId": userId,
"name": name,
"guruzziId": guruzziId,
};
}
class UserDetail {
UserDetail({
this.id,
this.guruzziId,
this.firstName,
this.lastName,
this.photoUrl,
});
String id;
String guruzziId;
String firstName;
String lastName;
String photoUrl;
factory UserDetail.fromJson(Map<String, dynamic> json) => UserDetail(
id: json["_id"],
guruzziId: json["guruzziId"],
firstName: json["firstName"],
lastName: json["lastName"],
photoUrl: json["photoUrl"],
);
Map<String, dynamic> toJson() => {
"_id": id,
"guruzziId": guruzziId,
"firstName": firstName,
"lastName": lastName,
"photoUrl": photoUrl,
};
}
Это метод GET, в котором код ломается
static Future<List<Post>> getPosts() async {
try {
var response = await http.get('$_url/post?userId=$_userId');
if (response.statusCode == 200) {
var data = jsonDecode(response.body)['data'];
List<Post> userPosts = postFromJson(data.toString());
} else {
return List<Post>();
}
} catch (e) {
return List<Post>();
}
}
Тело ответа
{
"data": [
{
"_id": "5eedd6ee16bec833275bc929",
"caption": "Test",
"postImages": "",
"postVideos": "",
"postAudio": "",
"postSharedBy": [
""
],
"tag": [
{
"userId": "",
"name": "",
"guruzziId": ""
}
],
"location": "",
"locationLatLong": [
""
],
"anonymous": false,
"flagInappropriate": [
""
],
"likes": [
""
],
"userId": "5eedbd19996dc053453ff24c",
"createdAt": "2020-06-20 09:29:18",
"updatedAt": "2020-06-20 09:29:18",
"status": true,
"userDetail": {
"_id": "5eedbd19996dc053453ff24c",
"guruzziId": "_bxgza1vgw",
"firstName": "Archit Arora",
"lastName": "",
"photoUrl": ""
}
}
],
"error": false,
"message": "Success",
"code": 200
}