Storing the IconData on the server and retrieving it with the JsonSerializable, lead to the following error on the Jenkin android build.
This application cannot tree shake icons fonts. It has non-constant instances of IconData at the following locations:
The following code dto to build the icon data object:
@JsonSerializable(explicitToJson: true)
class DrawerIconDataDto extends IconData {
const DrawerIconDataDto(
super.codePoint, {
super.fontFamily,
super.fontPackage,
super.matchTextDirection,
});
/// Create a new instance from a json.
factory DrawerIconDataDto.fromJson(Map<String, dynamic> json) {
return _$DrawerIconDataDtoFromJson(json);
}
/// Convert this instance to a json.
@override
Map<String, dynamic> toJson() => _$DrawerIconDataDtoToJson(this);
}
Even adding const to the following class lead to the error. I tried to add a const to the factory but it is not possible.
How can I get a constant DataIcon object from the server using JsonSerializable and without modifying the Jenkins configuration (not allowed).
In Dart, const
means that the compiler can determine the value at compile time. Obviously, this does not apply to data retrieved from a server.
So, you cannot retrieve const
data from a server.
What you could do is have all possible IconData values as const
instances in you code, and retrieve an identifier from the server.