【问题标题】:Unhandled Exception: Converting object to an encodable object failed: _LinkedHashMap未处理的异常:将对象转换为可编码对象失败:_LinkedHashMap
【发布时间】:2021-05-05 08:22:48
【问题描述】:

我是 Flutter 的新手,我已经能够成功调用 api,但是当我通过添加 map 修改我的模型类时,我得到以下异常 未处理的异常:将对象转换为可编码对象失败:_LinkedHashMap

我的模型类参考如下

class Profile {
  String id;
  String userId;
  String username;
  String password;
  String email;
  String arabicFullNam;
  String englishFullNam;
  String phoneNumber;
  String civilId;
  int type;
  bool success;
  List<dynamic> errors;
  Map<String,dynamic>body;

  Profile({this.id, this.userId,this.username, this.password, this.arabicFullNam, this.englishFullNam, this.email,this.civilId, this.errors,this.success,this.type,this.body,this.phoneNumber});

  factory Profile.fromJson(Map<String, dynamic> map) {
    return Profile(
        id: map["id"], userId: map["userId"],phoneNumber: map["phoneNumber"],username: map["username"], password: map["password"], englishFullNam: map["fullnameEn"], arabicFullNam: map['fullnameAr'], email: map['email'],civilId:map['civilId'], errors: map['errors'], success: map['success'],type: map['type'],body: map['body']);
  }

  Map<String, dynamic> toJson() {
    return {"id": id, "userId": userId,"username": username, phoneNumber: "phoneNumber", "password": password, "fullnameEn": englishFullNam, "fullnameAr": arabicFullNam,"email": email, "civilId": civilId,"success": success, "type": type, "body": body};
  }

  @override
  String toString() {
    return 'Profile{id: $id,userId: $userId, username: $username, password: $password, email: $email, fullnameEn: $englishFullNam, fullnameAr: $arabicFullNam, civilId: $civilId, errors: $errors, success: $success, type: $type, body: $body, phoneNumber: $phoneNumber }';
  }


}

List<Profile> profileFromJson(String jsonData) {
  final data = json.decode(jsonData);
  return List<Profile>.from(data.map((item) => Profile.fromJson(item)));
}


Profile postFromJson(String str) {
  final jsonData = json.decode(str);
  return Profile.fromJson(jsonData);
}


String profileToJson(Profile data) {
  final jsonData = data.toJson();
  print("##--tojson"+jsonData.toString());
  return json.encode(jsonData);
}

任何帮助将不胜感激

【问题讨论】:

标签: flutter dart flutter-layout flutter-dependencies flutter-animation


【解决方案1】:

确保检查您传递或转换的原始 json。我遇到了 null:phoneNumber 而不是 phoneNumber:null 的问题。

【讨论】:

    【解决方案2】:

    试试这个

    class Profile {
      String id;
      String userId;
      String username;
      String password;
      String email;
      String arabicFullNam;
      String englishFullNam;
      String phoneNumber;
      String civilId;
      int type;
      bool success;
      List<dynamic> errors;
      Map<String, dynamic> body;
      Profile({
        this.id,
        this.userId,
        this.username,
        this.password,
        this.email,
        this.arabicFullNam,
        this.englishFullNam,
        this.phoneNumber,
        this.civilId,
        this.type,
        this.success,
        this.errors,
        this.body,
      });
    
      Map<String, dynamic> toMap() {
        return {
          'id': id,
          'userId': userId,
          'username': username,
          'password': password,
          'email': email,
          'arabicFullNam': arabicFullNam,
          'englishFullNam': englishFullNam,
          'phoneNumber': phoneNumber,
          'civilId': civilId,
          'type': type,
          'success': success,
          'errors': errors,
          'body': body,
        };
      }
    
      factory Profile.fromMap(Map<String, dynamic> map) {
        if (map == null) return null;
    
        return Profile(
          id: map['id'],
          userId: map['userId'],
          username: map['username'],
          password: map['password'],
          email: map['email'],
          arabicFullNam: map['arabicFullNam'],
          englishFullNam: map['englishFullNam'],
          phoneNumber: map['phoneNumber'],
          civilId: map['civilId'],
          type: map['type'],
          success: map['success'],
          errors: List<dynamic>.from(map['errors']),
          body: Map<String, dynamic>.from(map['body']),
        );
      }
    
      String toJson() => json.encode(toMap());
    
      factory Profile.fromJson(String source) =>
          Profile.fromMap(json.decode(source));
    
      @override
      String toString() {
        return 'Profile(id: $id, userId: $userId, username: $username, password: $password, email: $email, arabicFullNam: $arabicFullNam, englishFullNam: $englishFullNam, phoneNumber: $phoneNumber, civilId: $civilId, type: $type, success: $success, errors: $errors, body: $body)';
      }
    }
    
    

    【讨论】:

    • 尝试替换上面的factory Profile.fromJson(Map&lt;String, dynamic&gt; map)。它基本上来自那个方法。我刚刚添加了这一行body: Map&lt;String, dynamic&gt;.from(map['body'])
    • 能不能把上面的原始json数据加进去。
    • 当然.... ##--tojson{id: null, userId: null, username: gvv, null: phoneNumber, password: 5677v, fullnameEn: null, fullnameAr: null, email: null ,civilId:空,成功:空,类型:空,正文:空}
    • 我编辑了我的答案并创建了模型。这就是我能提供的全部帮助
    • 非常感谢..但是我想通了。但是问题是我在传递时给出了值而不是键,因此原始 json 在传递时具有 null:phoneNumber .. 它已解决。真的很感谢你的手势。
    猜你喜欢
    • 2019-11-06
    • 2020-07-18
    • 2021-10-05
    • 2021-04-08
    • 2021-04-16
    • 2020-03-21
    • 2018-09-20
    • 2021-11-04
    • 2020-08-21
    相关资源
    最近更新 更多