FieldModel.fromJson constructor

FieldModel.fromJson(
  1. Map<String, dynamic> json, {
  2. dynamic answer,
})

Implementation

factory FieldModel.fromJson(Map<String, dynamic> json, {dynamic answer}) {
  var optionsList = json['options'] as List?;
  var dependenciesJson = json['dependencies'] as Map<String, dynamic>?;

  // Using null safety operators
  String fId = json['fId'] as String? ?? 'defaultFId';
  String type = json['type'] as String? ?? 'defaultType';
  String text = json['text'] as String? ?? 'defaultText';

  return FieldModel(
    fId: fId,
    type: type,
    text: text,
    defaultValue: json['default'],
    options: optionsList?.map((i) => Option.fromJson(i)).toList(),
    isRequired: json['isRequired'] ?? false,
    isVisible: json['isVisible'] ?? true,
    dependencies: dependenciesJson != null
        ? Dependency.fromJson(dependenciesJson)
        : null,
    currentAnswer: answer ?? json['default'],
  );
}