I want to set a default value for AvailableService
. It straight forward enough with primitives. How would I do it with a custom Object
class Submenu extends Equatable {
@JsonKey(defaultValue: "")
final String name;
@JsonKey(defaultValue: new AvailableService(false,false,false,false))
final AvailableService availableService;
}
the custom Object:
AvailableService {
bool supportsDelivery;
bool supportsTableService;
bool supportsCollection;
bool supportsChat;
}
And the compile time error is
Arguments of a constant creation must be constant expressions.
Try making the argument a valid constant, or use 'new' to call the constructor
In general, as an annotation argument you can only pass constants, so you cannot pass an object created with new but only with a const constructor (you should define all AvailableService fields as final, and define a const constructor). In json_Serializable, however, defaultValue currently has some additional constraints:
@JsonKey(defaultValue: {"supportsDelivery" : false, "supportsTableService" : false, /* etc */})
), but even this possibility is currently only usable for the default value of fields with Map type, and not for fields with custom types (like AvailableService). In this case, you not get an error, but the defaultValue will never be used (by looking at the code generated in the .g.dart file, you can understand why). There are open requests for this issue as well: #676 - Allow default value encoded as JSON and #789 - how to set default value to nested JSON objects?At the moment, therefore, until the aforementioned requests are followed up, the only workaround I have found is handle the defaultValue in the fromJson()
factory:
factory AvailableService.fromJson(Map<String, dynamic> json) =>
json != null
? _$AvailableServiceFromJson(json)
: AvailableService(false, false, false, false);
In short, JsonKey.defaultValue
currently only supports literals -not even accepting constants- and can only be used for the default value of primitive type fields (or List, Map, Set).