buildForm method

FormGroup buildForm(
  1. BeneficiaryRegistrationState state
)

Implementation

FormGroup buildForm(BeneficiaryRegistrationState state) {
  final household = state.mapOrNull(editHousehold: (value) {
    return value.householdModel;
  }, create: (value) {
    return value.householdModel;
  });

  final registrationDate = state.mapOrNull(
    editHousehold: (value) {
      return value.registrationDate;
    },
    create: (value) => DateTime.now(),
  );

  return fb.group(<String, Object>{
    _dateOfRegistrationKey:
        FormControl<DateTime>(value: registrationDate, validators: []),
    _memberCountKey: FormControl<int>(
      value: household?.memberCount ?? 1,
    ),
    _pregnantWomenCountKey: FormControl<int>(
      value: household?.additionalFields?.fields
                  .where((h) =>
                      h.key == AdditionalFieldsType.pregnantWomen.toValue())
                  .firstOrNull
                  ?.value !=
              null
          ? int.tryParse(household?.additionalFields?.fields
                  .where((h) =>
                      h.key == AdditionalFieldsType.pregnantWomen.toValue())
                  .firstOrNull
                  ?.value
                  .toString() ??
              '0')
          : 0,
    ),
    _childrenCountKey: FormControl<int>(
      value: household?.additionalFields?.fields
                  .where(
                      (h) => h.key == AdditionalFieldsType.children.toValue())
                  .firstOrNull
                  ?.value !=
              null
          ? int.tryParse(household?.additionalFields?.fields
                  .where(
                      (h) => h.key == AdditionalFieldsType.children.toValue())
                  .firstOrNull
                  ?.value
                  .toString() ??
              '0')
          : 0,
    )
  });
}