我是 flutter 的新手,在取得了一些不错的进展后,我认为应该是一个简单的循环。我可以显示来自 JSON 的单个评论,但无法显示所有评论。 下面是我可以获取和打印 JSON 响应的函数。
@override
void initState() {
getproduct(widget.idproduct);
super.initState();
}
Future<YourDataEntity> getproduct(int id) async {
var response = await Network().getData('/publication/show/$id');
return YourDataEntity.fromJson(response.body);
}
child: SingleChildScrollView(
child: FutureBuilder<YourDataEntity>(
future: getproduct(widget.idproduct),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot);
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
for (int i = 0; i < snapshot.data.length; i++)
CommentsList(
comment: snapshot.data[i]['comment'][i]
['comment'],
username: snapshot.data[i]['comment'][i]
['username'],
user_picture:
"${snapshot.data[i]['comment'][i]['user_picture']}",
)
],
);
}
return Center(
child: CircularProgressIndicator(),
);
},
下面是请求中的 JSON。我做错了什么?
{
"code": 0,
"message": {
"message": "Détails de publication"
},
"publication": [
{
"id": 47,
"user_id": 4,
"name": "spadrilles",
"description": "deux spadrille presque neufs",
"category": "Bijoux",
"quantity": 0,
"size": "XS 32",
"brand": "ZARA",
"for_who": "homme",
"color": "Blanc",
"delivery": "disponible le 25-01-2020",
"price": "150.5",
"progression": 0,
"discount": "5",
"visibility": 0,
"status": "Presque Neuf",
"softdelete": 0,
"created_at": "2021-01-06T14:51:27.000000Z",
"updated_at": "2021-01-22T10:26:39.000000Z",
"picture1": "b7c992f0ac7b0edd7e7e5ee12e617d9c07411b4d343f64f1caf94aa08d08b8fc.jpg",
"picture2": "efdffb16f47e0ab40788b760ab9154fc95a1fded8f6963b227aceaff302e8623.png",
"picture3": "8e23d6ec620353c8802bb969836b5d80f6c871fdc419f695a7c1da0e71e378ff.png",
"picture4": null,
"picture5": "5f3eac05c317d25cd0d691d545236fb0ef6059870217ea50dfd45b914d997d82.png",
"comment": [
{
"id": 3,
"pub_id": 47,
"user_id": 21,
"comment": "test test testsssssssssssst",
"created_at": "2021-03-25T09:40:32.000000Z",
"updated_at": "2021-03-25T09:40:32.000000Z",
"username": "occasion_saly",
"user_picture": "1611217814.png"
},
{
"id": 4,
"pub_id": 47,
"user_id": 21,
"comment": "test test testsssssssssssst",
"created_at": "2021-03-25T09:40:36.000000Z",
"updated_at": "2021-03-25T09:40:36.000000Z",
"username": "occasion_saly",
"user_picture": "1611217814.png"
},
{
"id": 5,
"pub_id": 47,
"user_id": 21,
"comment": "test test testsssssssssssst",
"created_at": "2021-03-25T09:40:37.000000Z",
"updated_at": "2021-03-25T09:40:37.000000Z",
"username": "occasion_saly",
"user_picture": "1611217814.png"
}
],
"ownerpicture": "1608970983.png"
}
],
"error": {},
"status": 200
}
答案 0 :(得分:0)
您的 snapshot.data
是一个对象 YourDataEntity
通过 snapshot.data.publication.length
尝试循环