Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialvalue wont change #283

Closed
softmarshmallow opened this issue May 18, 2020 · 1 comment
Closed

initialvalue wont change #283

softmarshmallow opened this issue May 18, 2020 · 1 comment

Comments

@softmarshmallow
Copy link

softmarshmallow commented May 18, 2020

formBuilderKey.currentState.value wont change, it always returns the provided initial value.

i'm passing formBuilderKey on parent widget to child widget. on child widget, it uses like widget.formBuilderKey. is it not a valid usecase?

parent.dart


Widget build(BuildContext context){

....
          CustomerEditBasicInfoCard(
            initialValue: data.toCustomerProfileBasicInformationSegment(),
            onChange: (c) {},
            formBuilderKey: formBuilderKey,
          ),
....
}


  void submitForm() {
    print("submitting form");
    final valid = formBuilderKey.currentState.validate();
    if (valid) {
      data.profile.lastName = formBuilderKey.currentState.value[CustomerFormKeys.lastName];
      data.profile.firstName = formBuilderKey.currentState.value[CustomerFormKeys.firstName];
      data.profile.phone = formBuilderKey.currentState.value[CustomerFormKeys.phone];
      data.type = formBuilderKey.currentState.value[CustomerFormKeys.type];
      data.profile.industry = formBuilderKey.currentState.value[CustomerFormKeys.lastName];
      print(formBuilderKey.currentState.value);
    }
  }


child.dart (CustomerEditBasicInfoCard)


  Widget _buildBody() {
    return FormBuilder(
        key: widget.formBuilderKey,
        initialValue: {
          CustomerFormKeys.birth: widget.initialValue.birth ?? DateTime.now(),
          CustomerFormKeys.firstName: widget.initialValue.firstName,
          CustomerFormKeys.lastName: widget.initialValue.lastName,
          CustomerFormKeys.phone: widget.initialValue.phoneNumber,
          CustomerFormKeys.type: widget.initialValue.type
        },
        autovalidate: true,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _buildTemplateSelection(),
            FormBuilderTextField(
              attribute: CustomerFormKeys.firstName,
              decoration: InputDecoration(hintText: "first name"),
              maxLines: 1,
              validators: [
                FormBuilderValidators.required(),
                FormBuilderValidators.maxLength(20),
              ],
            ),
            FormBuilderTextField(
              attribute: CustomerFormKeys.lastName,
              decoration: InputDecoration(hintText: "last name"),
              maxLines: 1,
              validators: [
                FormBuilderValidators.maxLength(20),
              ],
            ),
            FormBuilderTextField(
              attribute: CustomerFormKeys.phone,
              decoration: InputDecoration(hintText: "phone number"),
              validators: [
                FormBuilderValidators.maxLength(30),
                FormBuilderValidators.numeric(),
              ],
            ),
            FormBuilderDateTimePicker(
              attribute: CustomerFormKeys.birth,
              inputType: InputType.date,
              format: DateFormat("yyyy-MM-dd"),
              decoration: InputDecoration(labelText: "birthday"),
            ),
          ],
        ));
  }
@softmarshmallow
Copy link
Author

changing _fbKey.currentState.validate()
to _fbKey.currentState.saveAndValidate() worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant