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:
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];