ApiPost.fromJson constructor
Return ApiPost instance from json that comes from backend.
ApiPost.fromJson({}) // This works for test
You may want to clone a post. Then do the following;
ApiPost clone = ApiPost.fromJson(this.posts.first.data);
When you want to put a post into cart, the post must not be referenced. In that case,
/ 아이템 복사. 카트에 들어간 아이템은 변경이 되면 안되고, 동일한 상품도 중복으로 넣을 수 있어야 히기 때문에, 현재 아이템을 복사해서 카트에 넣어야 한다.
ApiPost clone = ApiPost.fromJson(item.data);
item.options.keys.forEach((k) {
/ ApiItemOption 도 레퍼런스로 연결되기 때문에, 복제를 해야 한다.
clone.options[k] = ApiItemOption(
count: item.options[k].count,
discountRate: item.options[k].discountRate,
price: item.options[k].price,
text: item.options[k].text);
});
}
Implementation
factory ApiPost.fromJson(Map<String, dynamic> json) {
final List<ApiFile> files = json["files"] == null || json["files"] == ''
? []
: List<ApiFile>.from(json["files"].map((x) => ApiFile.fromJson(x)));
return ApiPost(
data: json,
idx: "${json["idx"]}",
categoryId: json['categoryId'],
title: json["title"] ?? '',
content: json["content"] ?? '',
user: ApiShortUser.fromJson(json['user']),
/// Updates
userIdx: "${json['userIdx']}",
rootIdx: "${json['rootIdx']}",
parentIdx: "${json['parentIdx']}",
relationIdx: "${json['relationIdx']}",
categoryIdx: "${json['categoryIdx']}",
subcategory: json['subcategory'],
path: json['path'],
createdAt: "${json["createdAt"]}",
updatedAt: "${json["updatedAt"]}",
deletedAt: "${json["deletedAt"]}",
y: "${json['Y']}",
n: "${json['N']}",
private: json['private'],
privateTitle: json['privateTitle'],
privateContent: json['privateContent'],
appliedPoint: "${json['appliedPoint']}",
code: json['code'],
files: files,
comments:
json["comments"] == null ? [] : List<ApiComment>.from(json["comments"].map((x) => ApiComment.fromJson(x))),
// DateTime.parse(json["post_date"] ?? DateTime.now().toString())
/// Shopping Mall
shortTitle: json["shortTitle"],
price: _parseInt(json["price"]) ?? 0,
optionItemPrice: json["optionItemPrice"] == 'Y' ? true : false,
discountRate: _parseInt(json["discountRate"]),
pause: json["pause"] == 'Y' ? true : false,
point: json["point"] == null ? 0 : _parseInt(json["point"]),
volume: json["volume"],
deliveryFee: _parseInt(json["deliveryFee"]),
storageMethod: json["storageMethod"],
expiry: json["expiry"],
foodKind: json['food_kind'],
origin: json['origin'],
allergy: json['allergy'],
nutritiveComponents: json['nutritive_components'],
primaryPhoto: files.firstWhereOrNull((f) => f.code == 'primaryPhoto')?.idx ?? '',
widgetPhoto: files.firstWhereOrNull((f) => f.code == 'widgetPhoto')?.idx ?? '',
detailPhoto: files.firstWhereOrNull((f) => f.code == 'detailPhoto')?.idx ?? '',
bannerPhoto: files.firstWhereOrNull((f) => f.code == 'bannerPhoto')?.idx ?? '',
keywords: json['keywords'] ?? '',
options: _prepareOptions(json['options'], json["optionItemPrice"] == 'Y' ? true : false),
shortDateTime: json['shortDate'] ?? '',
);
}