我正在制作一个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,
};
}
}
我该怎么做?还有另一种方法吗? 谢谢
答案 0 :(得分:1)
您可以使用https://app.quicktype.io/来简化此密码
您需要的代码请参见下文
// To parse this JSON data, do
//
// final response1 = response1FromJson(jsonString);
import 'dart:convert';
List<Response1> response1FromJson(String str) => new List<Response1>.from(json.decode(str).map((x) => Response1.fromJson(x)));
String response1ToJson(List<Response1> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));
class Response1 {
int id;
String name;
String descriptions;
int price;
int discount;
String category;
int quantity;
String brand;
String image;
DateTime createdAt;
DateTime updatedAt;
Images images;
List<String> tags;
List<dynamic> sizes;
Response1({
this.id,
this.name,
this.descriptions,
this.price,
this.discount,
this.category,
this.quantity,
this.brand,
this.image,
this.createdAt,
this.updatedAt,
this.images,
this.tags,
this.sizes,
});
factory Response1.fromJson(Map<String, dynamic> json) => new Response1(
id: json["id"],
name: json["name"],
descriptions: json["descriptions"],
price: json["price"],
discount: json["discount"],
category: json["category"],
quantity: json["quantity"],
brand: json["brand"],
image: json["image"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
images: Images.fromJson(json["images"]),
tags: new List<String>.from(json["tags"].map((x) => x)),
sizes: new List<dynamic>.from(json["sizes"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"descriptions": descriptions,
"price": price,
"discount": discount,
"category": category,
"quantity": quantity,
"brand": brand,
"image": image,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
"images": images.toJson(),
"tags": new List<dynamic>.from(tags.map((x) => x)),
"sizes": new List<dynamic>.from(sizes.map((x) => x)),
};
}
class Images {
String url;
String color;
Images({
this.url,
this.color,
});
factory Images.fromJson(Map<String, dynamic> json) => new Images(
url: json["url"],
color: json["color"],
);
Map<String, dynamic> toJson() => {
"url": url,
"color": color,
};
}