【发布时间】:2021-06-27 08:42:45
【问题描述】:
我是 flutter 的初学者。我想使用 API 从 Complex json 获取数据。使用邮递员我的响应正文包含发布对象,其中我有我的对象列表。 .首先我创建了 Publication 模型,我尝试使用它来获取数据,但 snapshot.data 仍然是 NULL 。有什么帮助吗??
@override
void initState() {
getproduct(widget.idproduct);
super.initState();
}
Future<Publication> getproduct(int id) async {
var response = await Network().getData('/publication/show/$id');
return Publication.fromJson(json.decode(response.body['publication']));
}
child: SingleChildScrollView(
child: FutureBuilder<Publication>(
future: getproduct(widget.idproduct),
builder: (BuildContext context, AsyncSnapshot snapshot) {
inspect(snapshot.data);
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
for (int i = 0;
i < snapshot.data.publication.length;
i++)
CommentsList(
comment: snapshot.data.publication[i].comment,
)
],
);
}
这是我的出版类:
class Publication {
int id;
int userId;
String name;
String description;
String category;
int quantity;
String size;
String brand;
String forWho;
String color;
String delivery;
String price;
int progression;
String discount;
int visibility;
String status;
int softdelete;
String createdAt;
String updatedAt;
String picture1;
String picture2;
String picture3;
Null picture4;
String picture5;
List<Comment> comment;
String ownerpicture;
Publication(
{this.id,
this.userId,
this.name,
this.description,
this.category,
this.quantity,
this.size,
this.brand,
this.forWho,
this.color,
this.delivery,
this.price,
this.progression,
this.discount,
this.visibility,
this.status,
this.softdelete,
this.createdAt,
this.updatedAt,
this.picture1,
this.picture2,
this.picture3,
this.picture4,
this.picture5,
this.comment,
this.ownerpicture});
Publication.fromJson(Map<String, dynamic> json) {
id = json['id'];
userId = json['user_id'];
name = json['name'];
description = json['description'];
category = json['category'];
quantity = json['quantity'];
size = json['size'];
brand = json['brand'];
forWho = json['for_who'];
color = json['color'];
delivery = json['delivery'];
price = json['price'];
progression = json['progression'];
discount = json['discount'];
visibility = json['visibility'];
status = json['status'];
softdelete = json['softdelete'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
picture1 = json['picture1'];
picture2 = json['picture2'];
picture3 = json['picture3'];
picture4 = json['picture4'];
picture5 = json['picture5'];
if (json['comment'] != null) {
comment = new List<Comment>();
json['comment'].forEach((v) {
comment.add(new Comment.fromJson(v));
});
}
ownerpicture = json['ownerpicture'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['user_id'] = this.userId;
data['name'] = this.name;
data['description'] = this.description;
data['category'] = this.category;
data['quantity'] = this.quantity;
data['size'] = this.size;
data['brand'] = this.brand;
data['for_who'] = this.forWho;
data['color'] = this.color;
data['delivery'] = this.delivery;
data['price'] = this.price;
data['progression'] = this.progression;
data['discount'] = this.discount;
data['visibility'] = this.visibility;
data['status'] = this.status;
data['softdelete'] = this.softdelete;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
data['picture1'] = this.picture1;
data['picture2'] = this.picture2;
data['picture3'] = this.picture3;
data['picture4'] = this.picture4;
data['picture5'] = this.picture5;
if (this.comment != null) {
data['comment'] = this.comment.map((v) => v.toJson()).toList();
}
data['ownerpicture'] = this.ownerpicture;
return data;
}
}
打印(快照)时出现以下错误:
检查时出现此错误(快照);
【问题讨论】:
-
-
AsyncSnapshot
(ConnectionState.waiting, null, null, null) -
你没有正确解析 Json,它的类型不匹配,正如
String is not a subType of int所说的那样 -
我用 screeshoot 更新了问题。