0

I'm trying to import/export/store accumulated data in a json file. I want all keys to show in the json file with the value null when they have not been assigned something. So I kind of need an "empty shell" at the beginning.

This worked fine while I had all the values for the code generation in one class and used them directly. Now I want to group them into different nested classes but now I get this exception at runtime: enter image description here

Here is what worked as I intended:

@JsonSerializable(explicitToJson: true, includeIfNull: true)
class SystemTelemetry{
  SystemTelemetry(
    this.a,
    this.b,
    this.c,
  );
  
  @JsonKey(includeIfNull: true)
  String? a;
  int? b;
  int? c;
  
  factory SystemTelemetry.fromJson(Map<String, dynamic> json) => _$SystemTelemetryFromJson(json);
  
  Map<String, dynamic> toJson() => _$SystemTelemetryToJson(this);
}

class Common {
  //Container-class for common values & methods across routes (screens)
  static late SystemTelemetry systemTelemetry;
  ...
}

with accessing the members like so:

Common.systemTelemetry.a = data[0];

This is how I thought it should work when nesting the class (note I have more than just this one nested class):

@JsonSerializable(explicitToJson: true, includeIfNull: true)
class SystemTelemetry{
  SystemTelemetry(
    this.a,
    this.b,
    this.c,
  );
  
  @JsonKey(includeIfNull: true)
  String? a;
  int? b;
  int? c;
  
  factory SystemTelemetry.fromJson(Map<String, dynamic> json) => _$SystemTelemetryFromJson(json);
  
  Map<String, dynamic> toJson() => _$SystemTelemetryToJson(this);
}

@JsonSerializable(explicitToJson: true, includeIfNull: true)
class TelemetryData {
  TelemetryData(
    this.systemTelemetry, 
  );
  
  @JsonKey(includeIfNull: true)
  SystemTelemetry systemTelemetry = SystemTelemetry(null, null, null);
  
  factory TelemetryData.fromJson(Map<String, dynamic> json) => _$TelemetryDataFromJson(json);
  
  Map<String, dynamic> toJson() => _$TelemetryDataToJson(this);
}

class Common {
  //Container-class for common values & methods across routes (screens)
  static late TelemetryData telemetryData;
  ...
}

and then accessing the members like so:

Common.telemetryData.systemTelemetry.a = data[0];
EN20
  • 51
  • 11
  • Why don't you do a null check before you do the casting? Like this.. SystemTelemetry.fromJson(json['ystemTelemetry'] == null ? null : as Map) – Siddique Thanikad Jul 13 '22 at 15:32
  • @Siddique Assuming you mean the line where the exception occurres that is generated code I can try if changing that helps but the code generator will always overwrite what I'm adding. So I'd be looking for a solution to make the codegenerator write that line. – EN20 Jul 14 '22 at 08:37
  • okay. If you want null check to be generated by tool then make your class variable nullable. I mean change the line static late SystemTelemetry systemTelemetry; to static SystemTelemetry? systemTelemetry; Let me know if this works. – Siddique Thanikad Jul 14 '22 at 12:22
  • the line `static late SystemTelemetry systemTelemetry;` does not exist in the nested class version – EN20 Jul 14 '22 at 12:35

0 Answers0