当前位置: 首页 > 知识库问答 >
问题:

未处理的异常:类型'(动态)=>Null'不是数据模型中'f'的类型'(字符串,动态)=>无效'的子类型

谭曦
2023-03-14

我未处理此错误异常:type'(动态)=

模型类

class UserLogin {
  bool success;
  int statusCode;
  String code;
  String message;
  List<Data> data;

  UserLogin({
    this.code,
    this.success,
    this.message,
    this.statusCode,
    this.data,
  });

  UserLogin.fromJson(Map<String, dynamic>json){
    success = json['success'];
    statusCode = json['statusCode'];
    code = json['code'];
    message = json['message'];
    //data = json['data'] != null ? new Data.fromJson(json['data']) : null;
    
    if(json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v){
        data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson(){
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['success'] = this.success;
    data['statuCode'] = this.statusCode;
    data['code'] = this.code;
    data['message'] = this.message;

    if(this.data != null) {
      data['data'] = this.data.toList();
    }

    return data;
  }

  @override toString() => this.toJson().toString();
}


class Data {
  String token;
  int id;
  String email;
  String nicename;
  String firstname;
  String lastname;
  String displayName;

  Data(
      {this.token,
      this.id,
      this.email,
      this.nicename,
      this.firstname,
      this.lastname,
      this.displayName});
  
  Data.fromJson(Map<String, dynamic> json){
    token = json['token'];
    id = json['id'];
    email = json['email'];
    nicename = json['nicename'];
    firstname = json['firstname'];
    displayName = json['displayName'];
  }

  Map<String, dynamic> toJson(){
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['token'] = this.token;
    data['id'] = this.id;
    data['email'] = this.email;
    data['nicename'] = this.nicename;
    data['firstname'] = this.firstname;
    data['displayName'] = this.displayName;

    return data;
  }

  
  
}

调用api的异步函数

Future<UserLogin> loginCustomer(String username, String password ) async {
    //List<UserLogin> model = new List<UserLogin>();
    
   UserLogin model;

    try {
      var response = await Dio().post(
        Config.tokenURL,
        data:  {
          "username" : username,
          "password" : password,
        },
        options: new Options(
          headers: {
            HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded",
          },
        ),
      );

      if(response.statusCode == 200) {
        print(response.data);
        model = UserLogin.fromJson(response.data);
      }
    } on DioError catch (e) {
      print(e.message);
    }
    return model;
     
  }

Api的响应

{success: true, statusCode: 200, code: jwt_auth_valid_credential, message: Credential is valid, data: {token:***, id: 1, email: ***, nicename: ***, firstName: ***, lastName: ***, displayName: ****}}

更新代码

class UserLogin {
  bool success;
  int statusCode;
  String code;
  String message;
  Data data;

  UserLogin({
    this.code,
    this.success,
    this.message,
    this.statusCode,
    this.data,
  });

  UserLogin.fromJson(Map<String, dynamic>json){
    success = json['success'];
    statusCode = json['statusCode'];
    code = json['code'];
    message = json['message'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
    
  
  }

  Map<String, dynamic> toJson(){
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['success'] = this.success;
    data['statuCode'] = this.statusCode;
    data['code'] = this.code;
    data['message'] = this.message;

    if(this.data != null) {
      data['data'] = this.data.toJson();
    }

    return data;
  }

  @override toString() => this.toJson().toString();
}


class Data {
  String token;
  int id;
  String email;
  String nicename;
  String firstname;
  String lastname;
  String displayName;

  Data(
      {this.token,
      this.id,
      this.email,
      this.nicename,
      this.firstname,
      this.lastname,
      this.displayName});
  
  Data.fromJson(Map<String, dynamic> json){
    token = json['token'];
    id = json['id'];
    email = json['email'];
    nicename = json['nicename'];
    firstname = json['firstname'];
    displayName = json['displayName'];
  }

  Map<String, dynamic> toJson(){
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['token'] = this.token;
    data['id'] = this.id;
    data['email'] = this.email;
    data['nicename'] = this.nicename;
    data['firstname'] = this.firstname;
    data['displayName'] = this.displayName;

    return data;
  }

  
  
}

共有1个答案

严阳秋
2023-03-14

基于response.data,您的数据是一个对象,但您在UserLogin模型类中将其定义为List,这是错误的。

正确的应该是Data

 类似资料: