扑动模型设计模式

发布于 2025-02-13 05:58:59 字数 1081 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2025-02-20 05:58:59

抱歉,答案更长。

您有2个选项

  1. 添加这些额外的参数并使其无效。

class Product {
 int id;
 String title;
 double mrp;
 String? firstCharacterTitle;

 Product(this.id, this.title, this.mrp, {this.firstCharacterTitle});

 factory Product.fromJson(
   int id,
   String title,
   double mrp, {
   String? firstCharacterTitle,
 }) {
   return Product(id, title, mrp, firstCharacterTitle: firstCharacterTitle);
 }
}
  1. 或使用动态的东西,
class Product {
  int id;
  String title;
  double mrp;
  Map<String, dynamic> extraParams;

  Product(this.id, this.title, this.mrp, this.prms);

  factory Product.fromJson(int id, String title, double mrp, Map<String, dynamic> prms) {
    return Product(id, title, mrp, prms);
  }
}

我还建议使用 json_serializable 。因为您的frofjson方法并不是真正的fromjson,而是采用已经转换的JSON参数。

Sorry for longer answer.

You have 2 options

  1. adding those extra parameters and make them nullable.

class Product {
 int id;
 String title;
 double mrp;
 String? firstCharacterTitle;

 Product(this.id, this.title, this.mrp, {this.firstCharacterTitle});

 factory Product.fromJson(
   int id,
   String title,
   double mrp, {
   String? firstCharacterTitle,
 }) {
   return Product(id, title, mrp, firstCharacterTitle: firstCharacterTitle);
 }
}
  1. Or work with something dynamic
class Product {
  int id;
  String title;
  double mrp;
  Map<String, dynamic> extraParams;

  Product(this.id, this.title, this.mrp, this.prms);

  factory Product.fromJson(int id, String title, double mrp, Map<String, dynamic> prms) {
    return Product(id, title, mrp, prms);
  }
}

I also recommend using json_serializable. Cause your fromJson method is not truly fromJson, but it's taking already converted Json parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文