代码之家  ›  专栏  ›  技术社区  ›  Farhana Naaz Ansari Debashish Dwivedi

调用api时如何处理map<dynamic>null对象

  •  0
  • Farhana Naaz Ansari Debashish Dwivedi  · 技术社区  · 5 年前

    如果发生以下情况,如何管理服务器响应 status 200 .

    @JsonSerializable(nullable: false)
    class LoginResponse {
      final String error;
      final int status;
      final List<User> userList;
      LoginResponse({this.error, this.status, this.userList});
    
      factory LoginResponse.fromJson(Map repJson){
    
    List<dynamic> userListResp=repJson['userData'];
    List<User> userList=userListResp.map((e)=>User.fromUser(e)).toList();
    int s=repJson['status'];
       if(s==200){
         return LoginResponse(error:repJson['error'],status: repJson['status'],userList:userList);
       } else{
         return LoginResponse(error:repJson['error'],status: repJson['status']);
       }}}
    
      class User{
      String cust_id;
      String cust_name;
      String cust_email;
      String cust_mob;
    
      User({this.cust_id,this.cust_name,this.cust_email,this.cust_mob});
      factory User.fromUser(Map userJson){
        return User(cust_id: userJson['cust_id'],cust_name: userJson['cust_name'],
            cust_email: userJson['cust_email'],cust_mob: userJson['cust_mob']);
      }
    }
    

    发生错误时的服务器响应

    {"error":"1","status":201,"message":"Entered email id already exist in our records"}
    

      {
    "error":"0",
    "status":200,
    "userData":[
     {
        "cust_id":"87",
        "cust_name":"kio",
        "cust_email":"kio1@kio.com",
        "cust_gend":null,
        "cust_dob":null,
        "cust_mob":"098998899889588",
        "cust_pass":"e10adc3949ba59abbe56e057f20f883e",
        "cust_age":null,
        "device_type":"android",
        "device_token":"eNWqzDwxqsQ:APA91bF-uK1MI11D3SgHGSw7Omv1imjDrPKBBCrN9JgmyJppHsNVeG5l56EkCCd5ZMaxL_ehQzVhtoEj0fTNB55wYGJt5BqYVvwfAb7HrBqwb_21M6VFPuF6LQINkvE1offQgZYweROO",
        "status":"0",
        "createAt":"2019-01-31 18:45:19",
        "updatedAt":"0000-00-00 00:00:00",
        "login_type":"",
        "login_id":null,
        "is_guest":"0",
        "auth_token":"",
        "forgot_token":null
     }]
    }
    

    当用户数据不存在或为空时,我如何管理?我尝试管理 status code is 201

    NoSuchMethodError:对null调用了方法“map”。

    1 回复  |  直到 5 年前
        1
  •  2
  •   Oleksii Kalentiev    5 年前

    要修复代码,请在if块内移动userList映射。这样,您将只解析状态代码为200的响应。

    int s=repJson['status'];
    if (s==200) {
      List<dynamic> userListResp=repJson['userData'];
      List<User> userList=userListResp.map((e)=>User.fromUser(e)).toList();
      return LoginResponse(error:repJson['error'], status:repJson['status'], userList:userList);
    } else {
      return LoginResponse(error:repJson['error'], status:repJson['status']);
    }
    

    但是,您可能不希望处理模型中的错误。最好在执行请求后检查错误,然后决定是否要解析响应。

    final response = await client.get(requestUrl);
    if (response.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      final loginResponse = LoginResponse.fromJson(json.decode(response.body));
    
      // ...
    } else {
      // If that call was not successful, throw an error or parse the error object.
      throw Exception('Failed to login');
    
      // ...
    }