扑动模型设计模式
我有一个product
模型,
class Product {
int id;
String title;
double mrp;
Product(this.id, this.title, this.mrp);
factory Product.fromJson(int id, String title, double mrp) {
return Product(id, title, mrp);
}
}
当我获取json
数据作为服务器的响应时,我有不同的JSON结构 -
第一个示例
{
"id": 523,
"title": "test",
"mrp": 23.25
}
第二个示例:第三个示例:
{
"id" : 523,
"title" : "test",
"mrp" : 23.25
"discountedPrice" : 23.00
}
第4个示例:
{
"id" : 523,
"title" : "test",
"mrp" : 23.25
"availableForSale" : true
}
第4个示例:
{
"id" : 523,
"title" : "test",
"mrp" : 23.25
"firstCharacterTitle" : "T"
}
当您可以看到我有不同的新字段,例如discountedprice
first charactertitle
可用
api结构API
。在其他情况下,可能还有许多其他。
在不同情况下,我应该使用产品
模型,因为我只有3个永久字段id
title
mrp
,但全部其他人依赖场景?我应该在模型或其他任何其他方面继续增加领域吗?最好的做法是什么?
i have a Product
Model
class Product {
int id;
String title;
double mrp;
Product(this.id, this.title, this.mrp);
factory Product.fromJson(int id, String title, double mrp) {
return Product(id, title, mrp);
}
}
I have different json structure for API calls when i get json
data as response from server -
1st Example
{
"id": 523,
"title": "test",
"mrp": 23.25
}
2nd Example :
{
"id" : 523,
"title" : "test",
"mrp" : 23.25
"discountedPrice" : 23.00
}
3rd Example :
{
"id" : 523,
"title" : "test",
"mrp" : 23.25
"availableForSale" : true
}
4th Example :
{
"id" : 523,
"title" : "test",
"mrp" : 23.25
"firstCharacterTitle" : "T"
}
As you can see i have different new field like discountedPrice
firstCharacterTitle
availableForSale
API structure in different fetching call of API
. and it can be many others in other scenario.
What should i do with Product
Model in different cases as i have only 3 permanent fields id
title
mrp
but all others are dependent on scenario ? Should i keep increasing field in my model or anything else? What is the best Practise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,答案更长。
您有2个选项
我还建议使用 json_serializable 。因为您的frofjson方法并不是真正的fromjson,而是采用已经转换的JSON参数。
Sorry for longer answer.
You have 2 options
I also recommend using json_serializable. Cause your fromJson method is not truly fromJson, but it's taking already converted Json parameters.