Given an unmodifiable JSON you get from the server like this:
{ "name": "someName",
"fields": [
{ "name": "field1",
"type": "text",
"subtype": "autofill",
"value": "someVal" },
{ "name": "field2",
"type": "text",
"subtype": "password",
"value": "somePass" },
{ "name": "field3",
"type": "number",
"subtype": "integer",
"value": 12 },
{ "name": "field4",
"type": "number",
"subtype": "double",
"value": 3.14 }
],
"etc": "etc"
}
The freezed model should be something like this:
@freezed
class ExampleSuperclass with _$ExampleSuperclass {
factory ExampleSuperclass({
String? name,
List<Field>? fields,
String? etc,
}) = _ExampleSuperclass;
factory ExampleSuperclass.fromJson(Map<String, dynamic> json) =>
_$ExampleSuperclassFromJson(json);
}
@Freezed(unionKey: 'type')
class Field with _$Field {
factory Field({
String? name,
String? type,
String? subtype,
}) = _Field;
factory Field.text({
String? name,
String? type,
String? subtype,
}) = _FieldText;
factory Field.number({
String? name,
String? type,
String? subtype,
}) = _FieldNumber;
factory Field.fromJson(Map<String, dynamic> json) => _$FieldFromJson(json);
}
But the you realize that you need both type and subtype in order to make more subtypes of Field.number and Field.text, how can you achieve a custom fromJson to have that done?