This is similar to issue #918 in that it is using extension of a base class, but is actually quite different. I have a situation where I have a base class (a business layer entity) which I am extending in the network layer in order to deserialise the server supplied JSON. The names of the properties in the server-side JSON is pretty verbose and I want to use shorter names in the business entity layer, I therefore need to find a way to tell json_deserialize to map the verbosely named json property into the entity property name.
Given the following:-
class Base {
final int name;
Base({required this.name});
}
@JsonSerializable()
class BaseModel extends Base {
BaseModel({required int reallyVeryVerboseName}) :
super(name: reallyVeryVerboseName);
int get reallyVeryVerboseName => name;
@override
@JsonKey(ignore: true)
int get name;
factory BaseModel.fromJson(Map<String, dynamic> json) =>
_$BaseModelFromJson(json);
}
it works flawlessly, e.g. I need to supply a getter for the verbose name and override the getter for the simple name and the following code is generated:-
However, I don't need to serialise the BaseModel therefore added createToJson: false and expected to be able to remove the two getter methods, e.g.
class Base {
final int name;
Base({required this.name});
}
@JsonSerializable(createToJson: false)
class BaseModel extends Base {
BaseModel({required int reallyVeryVerboseName}) :
super(name: reallyVeryVerboseName);
factory BaseModel.fromJson(Map<String, dynamic> json) =>
_$BaseModelFromJson(json);
}
but when I do this I get the following exception:-
[SEVERE] json_serializable:json_serializable on lib/models.dart:
Cannot populate the required constructor argument: reallyVeryVerboseName.
package:test/models.dart:159:3
╷
9 │ BaseModel({required int reallyVeryVerboseName}) :
│ ^
╵
Leaving the two getter methods in place and adding createToJson: false, generates the following:-
This is similar to issue #918 in that it is using extension of a base class, but is actually quite different. I have a situation where I have a base class (a business layer entity) which I am extending in the network layer in order to deserialise the server supplied JSON. The names of the properties in the server-side JSON is pretty verbose and I want to use shorter names in the business entity layer, I therefore need to find a way to tell
json_deserialize
to map the verbosely named json property into the entity property name.Given the following:-
it works flawlessly, e.g. I need to supply a getter for the verbose name and override the getter for the simple name and the following code is generated:-
However, I don't need to serialise the
BaseModel
therefore addedcreateToJson: false
and expected to be able to remove the two getter methods, e.g.but when I do this I get the following exception:-
Leaving the two getter methods in place and adding
createToJson: false
, generates the following:-So it seems that even when setting
createToJson: false
the getters are still required even though they are not used in the generated code.So, Is this a bug? Or am I missing something fundamental?
I'm really very new to dart/flutter - I've only been looking for about a week so it may well just be lack of knowledge / understanding on my part.