简体   繁体   English

如何解决未处理的异常:将对象转换为可编码对象失败?

[英]how to solve Unhandled Exception: Converting object to an encodable object failed?

I created 2 classes the first one is :我创建了 2 个类,第一个是:

class Mesure {
  late String equipement; 
  late String number; 
  late String courant = ""; 
  

  Mesure.init(List<String> values) {
    equipement = values[0];
    number = values[1];
    courant = values[2];
    
  }

}

and DataBase class :和数据库类:

class DataBase {
  late int? id;
  late Mesure? mesure;
  late String? status;
  DataBase.init(int id, Mesure mesure, String status) {
    id = id;
    mesure = mesure;
    status = status;
  }
}

Now i initialized DataBase with some values :现在我用一些值初始化了数据库:

  Mesure mesure = Mesure.init(values);
  DataBase test = DataBase.init(0, mesure, "status");

and i want to store it in storage getX :我想将它存储在存储 getX 中:

   final box = GetStorage();
  box.write('data', test);

but it does not work and throw an exception :但它不起作用并引发异常:

E/flutter (19759): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'DataBase' E/flutter (19759): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] 未处理异常:将对象转换为可编码对象失败:“数据库”实例

get_storage package uses json.encode and json.decode to save and load data from a file. get_storage包使用json.encodejson.decode来保存和加载文件中的数据。 And json.encode / json.decode documentation says that it can only serialize/deserialize classes if the toJson and fromJson functions are implemented respectively.并且json.encode / json.decode文档说,如果分别实现了toJsonfromJson函数,它只能序列化/反序列化类。

So, the code is going to look like this:因此,代码将如下所示:

class Mesure {
  late String equipement;
  late String number;
  late String courant = "";

  Mesure.init(List<String> values) {
    equipement = values[0];
    number = values[1];
    courant = values[2];
  }

  Mesure.fromJson(Map<String, dynamic> json)
      : equipement = json['equipement'],
        number = json['number'],
        courant = json['courant'];

  Map<String, dynamic> toJson() {
    return {
      'equipement': equipement,
      'number': number,
      'courant': courant,
    };
  }
}

class DataBase {
  final int id;
  final Mesure mesure;
  final String status;
  DataBase.init(this.id, this.mesure, this.status);

  DataBase.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        mesure = Mesure.fromJson(json['mesure']),
        status = json['status'];

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'mesure': mesure.toJson(),
      'status': status,
    };
  }
}

please try this请试试这个

class DataBase {
  int? id;
  Mesure? mesure;
  String? status;
  DataBase(this.id, this.mesure, this.status);
}

Then use然后使用

Mesure mesure = Mesure.init(values);
DataBase test = DataBase(id:0, mesure : mesure, status:"status");

暂无
暂无

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

相关问题 Flutter:未处理的异常:将对象转换为可编码对象失败:“AddProjectModel”的实例 - Flutter: Unhandled Exception: Converting object to an encodable object failed: Instance of 'AddProjectModel' 未处理的异常:将 object 转换为可编码的 object 失败:“测量”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'Mesure' 未处理的异常:将对象转换为可编码对象失败:“产品”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'Product' 未处理的异常:将 object 转换为可编码的 object 失败:“LoginModel”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'LoginModel' 未处理的异常:将 object 转换为可编码的 object 失败:“日期时间”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'DateTime' 未处理的异常:将 object 转换为可编码的 object 失败:“TextEditingController”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'TextEditingController' 未处理的异常:将对象转换为可编码对象失败:摄影 - Unhandled Exception: Converting object to an encodable object failed: Photography 未处理的异常:将 object 转换为可编码的 object 失败:_LinkedHashMap - Unhandled Exception: Converting object to an encodable object failed: _LinkedHashMap 未处理的异常:将 object 转换为可编码的 object 失败:“XFile”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'XFile' 我在 flutter 中收到错误“未处理的异常:将 object 转换为可编码的 object 失败:Null” - I get the error 'Unhandled Exception: Converting object to an encodable object failed: Null' in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM