【发布时间】:2019-12-15 21:32:14
【问题描述】:
我正在制作一个 API 来显示产品图片和颜色, 每当用户按下颜色按钮时,与该颜色关联的图像应使用 PageView.builder 显示。 如果这是我的 json,我如何为 api 制作模型工厂?
[
{
"id": 7,
"name": "1",
"descriptions": "1",
"price": 1,
"discount": 1,
"category": "new",
"quantity": 1,
"brand": "1",
"image": "",
"created_at": "2019-08-04 09:07:25",
"updated_at": "2019-08-04 09:07:25",
"images": {
"url":"1564909645iKiw2LkoEcQIIhB4MTZJTUfwTREleWH4wEuvmRPd.png",
"color":"#fffddd"
},
"tags": [
"large"
],
"sizes": []
}
]
我的模型和工厂:
class Response1 {
final String createdAt;
final int id;
final int quantity;
final double price;
final String name;
final String descriptions;
final String updatedAt;
final String image;
final int weight;
final List images;
final List tags;
final List sizes;
Response1(
{this.createdAt,
this.id,
this.quantity,
this.price,
this.name,
this.descriptions,
this.updatedAt,
this.image,
this.weight,
this.images,
this.tags,
this.sizes});
factory Response1.fromJson(Map<String, dynamic> json) {
return Response1(
createdAt: json['created_at'] as String,
id: json['id'] as int,
quantity: json['quantity'] as int,
price: _toDouble(json['price']),
name: json['name'] as String,
updatedAt: json['updated_at'] as String,
image: json['image'] as String,
descriptions: json['descriptions'] as String,
weight: json['weight'] as int,
images: json['images'] as List,
tags: json['tags'] as List,
sizes: json['sizes'] as List,
);
}
Map<String, dynamic> toJson() {
return {
'created_at': createdAt,
'id': id,
'quantity': quantity,
'price': price,
'name': name,
'updated_at': updatedAt,
'image': image,
'weight': weight,
'images': images,
'tags': tags,
'sizes': sizes,
};
}
}
我该怎么做?还有其他方法吗? 谢谢
【问题讨论】:
-
您正在寻找图像列表中目前只有一个对象?
标签: json laravel api button flutter