简体   繁体   English

如何处理 API 响应 model class 如果 object 在某些情况下不可用 API Flutter Dart

[英]How to Handle API response model class if object is not Available in some situation on Same API Flutter Dart

I need an Help for API response handling.我需要 API 响应处理的帮助。 I've created model class to convert API res.我创建了 model class 来转换 API res。 My issue is on Type the API response is changed for example我的问题是键入 API 响应已更改,例如

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在数据 Class 中 Object 会根据类型改变

Error log:错误日志:

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

Model Class: 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,
      };
}

I think problem is in null checking:我认为问题出在 null 检查中:

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抱歉之前的回答,我忘了检查 null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dart中的json API响应错误如何处理? - How to handle json API response errors in dart? Dart Flutter Generic Api 响应类动态类数据类型 - Dart Flutter Generic Api Response Class Dynamic Class Data Type Flutter:如何将Json转换为dart中的model class - Flutter: How to convert Json to model class in dart 如何为重型 api 响应 json flutter 制作 model - How to make model for heavy api response json flutter 如何将整个 json 响应存储在 dart 模型中并通过颤振中的 dart 模型实例访问该响应 - How to whole json response store in dart model and access that response via dart model instance in flutter 如何从 flutter 中的 api 响应中获取数组中的 object - How to get the object from array from api response in flutter 如何 model class 以序列化 Dart Z497031794414A553B435F90151ZAC<string, list> ) 进入 Flutter 中的 JSON?</string,> - How to model class in order to serialize Dart Object (Map<String, List>) into JSON in Flutter? How can I map json data from API into my own dart model without declare any class property in my dart class - How can I map json data from API into my own dart model without declare any class property in my dart class How to create pojo class of Object of ArrayList from REST API response? - How to create pojo class of Object of ArrayList from REST API response? 如何使用 observable 方法处理 Flutter GetX 中复杂的 API 数据响应 - How to handle complex API data response in Flutter GetX using observable method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM