简体   繁体   中英

Unhandled Exception: Converting object to an encodable object failed: Instance of 'Mesure'

hello i have the class mesure when i read data i use it ass a model then i save it on my database getstorage getX. it works perfectly, but when i add this test if mode = display i 'm going to add more data on my model and do the mapping. i'm getting this error. is it possible to skip "debut" and "fin" if it does not available?

Unhandled Exception: Converting object to an encodable object failed: Instance of 'Mesure' how can i fix it

class Mesure {
  late int id;
  String status;
  late String mode;
  late String equipementNumero;
  late String temperature;
  late String courant = "";
  late String debut;
  late String fin = "";

  Mesure.init(List<String> values, this.id, this.status) {
    mode = values[0];
    equipementNumero = values[1];
    temperature = values[2];
    courant = values[3];

    if (mode == "display") {
      debut = values[4];
      fin = values[5];
    }
  }

  Mesure.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        status = json['status'],
        mode = json['mode'],
        temperature = json['temperature'],
        equipementNumero = json['equipementNumero'],
        courant = json['courant'],
        debut = json['debut'],
        fin = json['fin'];

  Map<String, dynamic> toJson() {
    if (mode == "display") {
      return {
        'id': id,
        'status': status,
        'mode': mode,
        'temperature': temperature,
        'equipementNumero': equipementNumero,
        'courant': courant,
      };
    } else {
      return {
        'id': id,
        'status': status,
        'mode': mode,
        'temperature': temperature,
        'equipementNumero': equipementNumero,
        'courant': courant,
        'debut': debut,
        'fin': fin,
      };
    }
  }
}

There are 2 things:

  1. You have to initialize debut to make it write to JSON ( get_storage );
  2. And, you have to default the values of debut and fin to something when reading from JSON (get_storage);

So, the entire class Mesure would look like this:


class Mesure {
  late int id;
  String status;
  late String mode;
  late String equipementNumero;
  late String temperature;
  late String courant;
  late String debut = "";
  late String fin = "";

  Mesure.init(List<String> values, this.id, this.status) {
    mode = values[0];
    equipementNumero = values[1];
    temperature = values[2];
    courant = values[3];

    if (mode == "display") {
      debut = values[4];
      fin = values[5];
    }
  }

  Mesure.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        status = json['status'],
        mode = json['mode'],
        temperature = json['temperature'],
        equipementNumero = json['equipementNumero'],
        courant = json['courant'],
        debut = json['debut'] ?? '',
        fin = json['fin'] ?? '';

  Map<String, dynamic> toJson() {
    if (mode == "display") {
      return {
        'id': id,
        'status': status,
        'mode': mode,
        'temperature': temperature,
        'equipementNumero': equipementNumero,
        'courant': courant,
      };
    } else {
      return {
        'id': id,
        'status': status,
        'mode': mode,
        'temperature': temperature,
        'equipementNumero': equipementNumero,
        'courant': courant,
        'debut': debut,
        'fin': fin,
      };
    }
  }
}

Also, the code in the constructor looks inverted in toJson . Shouldn't in the constructor be like this mode = "display" instead of mode == "display" :

  Mesure.init(List<String> values, this.id, this.status) {
    mode = values[0];
    equipementNumero = values[1];
    temperature = values[2];
    courant = values[3];

    if (mode != "display") {
      debut = values[4];
      fin = values[5];
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM