如果在某些情况下在同一API飞镖上不可用对象,如何处理API响应模型类

发布于 2025-01-27 02:21:00 字数 2413 浏览 2 评论 0原文

我需要帮助API响应处理。我创建了模型类来转换API RES。 我的问题是在type上进行的API响应会更改,例如

如果type ='application'

 {
        "status": "success",
        "message": "Active loan Fetched",
        "data": {
            "Application": {
                "id": "",
                "coapp_status": null,
                "status": "pending-document",
                "approved_amount": "3000",
                "processing_fees": "450",
            },
            "type": "Application"
        },
        "timestamp": "2022-05-07 13:05:36",
        "require_update": "yes"
    }

,如果type ='loan'

  {
        "status": "success",
        "message": "Loan Fetched",
        "data": {
            "Loan": {
                "id": "",
                "policy_id": "",
                "application_id": "",
                "user_id": "",
                "approved_amount": "8000",
                "disbursment_amount": "6552",
                "processing_fees": "1200",
                "interest_rate": "480",
                "pre_emi_interest": "31.5616",
                "totalAmount": 8480,
                "gst_processing_fees": 216,
                "totalDisbursment": 6552.4384,
                "current_emi_date": "10 Jun 22",
                "current_emi_amount": "5088",
                "current_emi_id": "51298",
                
                "viewid": "",
                "customermessage": "Your loan have been approved for Rs.8000"
            },
            "type": "Loan"
           
     
    }

在数据类中,该对象将根据类型进行更改

错误日志:

Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>'

模型类:

  Application? application;
  Loan? loan;
  String? type;
  String? sanctionLetter;

  factory Data.fromJson(Map<String?, dynamic> json) => Data(
        application: Application.fromJson(json["Application"]), => **Error if application not Found**
        loan: Loan.fromJson(json["Loan"]), **Error if Loan not found**
        type: json["type"],
        sanctionLetter: json["sanction_letter"],
      );

  Map<String?, dynamic> toJson() => {
        "Application": application!.toJson(),
        "Loan": loan!.toJson(),
        "type": type,
        "sanction_letter": sanctionLetter,
      };
}

I need an Help for API response handling. I've created model class to convert API res.
My issue is on Type the API response is changed for example

if type = 'Application'

 {
        "status": "success",
        "message": "Active loan Fetched",
        "data": {
            "Application": {
                "id": "",
                "coapp_status": null,
                "status": "pending-document",
                "approved_amount": "3000",
                "processing_fees": "450",
            },
            "type": "Application"
        },
        "timestamp": "2022-05-07 13:05:36",
        "require_update": "yes"
    }

and If type = 'Loan'

  {
        "status": "success",
        "message": "Loan Fetched",
        "data": {
            "Loan": {
                "id": "",
                "policy_id": "",
                "application_id": "",
                "user_id": "",
                "approved_amount": "8000",
                "disbursment_amount": "6552",
                "processing_fees": "1200",
                "interest_rate": "480",
                "pre_emi_interest": "31.5616",
                "totalAmount": 8480,
                "gst_processing_fees": 216,
                "totalDisbursment": 6552.4384,
                "current_emi_date": "10 Jun 22",
                "current_emi_amount": "5088",
                "current_emi_id": "51298",
                
                "viewid": "",
                "customermessage": "Your loan have been approved for Rs.8000"
            },
            "type": "Loan"
           
     
    }

In Data Class The Object will be changed based on type

Error log:

Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>'

Model Class:

  Application? application;
  Loan? loan;
  String? type;
  String? sanctionLetter;

  factory Data.fromJson(Map<String?, dynamic> json) => Data(
        application: Application.fromJson(json["Application"]), => **Error if application not Found**
        loan: Loan.fromJson(json["Loan"]), **Error if Loan not found**
        type: json["type"],
        sanctionLetter: json["sanction_letter"],
      );

  Map<String?, dynamic> toJson() => {
        "Application": application!.toJson(),
        "Loan": loan!.toJson(),
        "type": type,
        "sanction_letter": sanctionLetter,
      };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜`诱少女 2025-02-03 02:21:00

我认为问题是在零检查中:

factory Data.fromJson(Map<String?, dynamic> json) => Data(
        application: json["Application"] ? Application.fromJson(json["Application"]) : null, // **Because it can be null**
        loan: json["Loan"] ? Loan.fromJson(json["Loan"]) : null, // **Because it can be null**
        type: json["type"],
        sanctionLetter: json["sanction_letter"],
      );

更新

application: json["Application"] != null ? Application.fromJson(json["Application"]) : null, // **Because it can be null**
loan: json["Loan"] != nul ? Loan.fromJson(json["Loan"]) : null,

对不起,我忘了检查null

I think problem is in null checking:

factory Data.fromJson(Map<String?, dynamic> json) => Data(
        application: json["Application"] ? Application.fromJson(json["Application"]) : null, // **Because it can be null**
        loan: json["Loan"] ? Loan.fromJson(json["Loan"]) : null, // **Because it can be null**
        type: json["type"],
        sanctionLetter: json["sanction_letter"],
      );

UPDATE

application: json["Application"] != null ? Application.fromJson(json["Application"]) : null, // **Because it can be null**
loan: json["Loan"] != nul ? Loan.fromJson(json["Loan"]) : null,

Sorry for previous answer, I forgot to check for null

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