In flutter, I'm trying to define a form field model. The form type is a enum, based on which different Field are to be returned.
My text field should look like this on toJson():
type : "text"
label : "..."
required : bool
minLength : int | null
maxLength : int | null
The model is defined like so, and uses json_serializable to auto generate the toJson and fromJson methods.
import 'package:json_annotation/json_annotation.dart';
part 'form_model.g.dart';
enum FormFieldType {
text,
number,
}
@JsonSerializable(createFactory: false)
abstract class FormFieldModel {
final String label;
final FormFieldType type;
final bool required;
FormFieldModel({required this.label, required this.type, this.required = true});
static FormFieldModel fromJson(Map<String, dynamic> json) {
final type = FormFieldType.values.firstWhere(
(e) => e.toString() == 'FormFieldType.${json['type']}',
orElse: () => throw Exception('Unknown form field type: ${json['type']}'),
);
switch (type) {
case FormFieldType.text:
return WizTextField.fromJson(json);
case FormFieldType.number:
return WizNumberField.fromJson(json);
}
}
}
@JsonSerializable()
class WizTextField extends FormFieldModel {
final int? minLength;
final int? maxLength;
WizTextField({
required String label,
this.minLength,
this.maxLength,
bool required = true,
}) : super(label: label, type: FormFieldType.text, required: required);
factory WizTextField.fromJson(Map<String, dynamic> json) => _$WizTextFieldFromJson(json);
@override
Map<String, dynamic> toJson() => _$WizTextFieldToJson(this);
}
// Similarly for Number
However, when I run dart run build_runner build, to my surprise, I find the 'type' field to be missing.
Map<String, dynamic> _$WizTextFieldToJson(WizTextField instance) =>
<String, dynamic>{
'label': instance.label,
'required': instance.required,
'minLength': instance.minLength,
'maxLength': instance.maxLength,
};
Where am I going wrong?
EDIT 1
(Based comments from one of the User)
I tried updating required field to isRequired but the problem persists. Putting complete code for reference (temp.dart)
import 'package:json_annotation/json_annotation.dart';
part 'temp.g.dart';
enum FormFieldType {
text,
number,
}
@JsonSerializable(createFactory: false)
abstract class FormFieldModel {
final String label;
final FormFieldType type;
final bool isRequired;
FormFieldModel({required this.label, required this.type, this.isRequired = true});
static FormFieldModel fromJson(Map<String, dynamic> json) {
final type = FormFieldType.values.firstWhere(
(e) => e.toString() == 'FormFieldType.${json['type']}',
orElse: () => throw Exception('Unknown form field type: ${json['type']}'),
);
switch (type) {
case FormFieldType.text:
return WizTextField.fromJson(json);
case FormFieldType.number:
return WizNumberField.fromJson(json);
}
}
}
@JsonSerializable()
class WizTextField extends FormFieldModel {
final int? minLength;
final int? maxLength;
WizTextField({
required String label,
this.minLength,
this.maxLength,
bool isRequired = true,
}) : super(label: label, type: FormFieldType.text, isRequired: isRequired);
factory WizTextField.fromJson(Map<String, dynamic> json) => _$WizTextFieldFromJson(json);
@override
Map<String, dynamic> toJson() => _$WizTextFieldToJson(this);
}
@JsonSerializable()
class WizNumberField extends FormFieldModel {
final int? min;
final int? max;
WizNumberField({
required String label,
this.min,
this.max,
bool isRequired = true,
}) : super(label: label, type: FormFieldType.number, isRequired: isRequired);
factory GWizNumberField.fromJson(Map<String, dynamic> json) => _$WizNumberFieldFromJson(json);
@override
Map<String, dynamic> toJson() => _$WizNumberFieldToJson(this);
}
This still gives same autogenerated toJson:
Map<String, dynamic> _$WizNumberFieldToJson(WizNumberField instance) =>
<String, dynamic>{
'label': instance.label,
'isRequired': instance.isRequired,
'min': instance.min,
'max': instance.max,
};