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

[General]: Log warning when patchValue does not find a corresponding registered field #1407

Open
1 task done
Camerash opened this issue Jun 24, 2024 · 0 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@Camerash
Copy link

Camerash commented Jun 24, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.3.0

What you'd like to happen

In the FormBuilderState.patchValue method, the code takes in the new value map, iteratively search for a registered field with the map key, and calls the field's didChange method if the field is found.

Developers that are not familiar with the inner workings with the library may often wrongly assume the patchValue method updates the value of the form, regardless whether the FormField is present or not.
This is often the case in my team of developers when we are using this library. Since conditional rendering is a very common practice in the Flutter framework, one may do something like this:

setState(() {
  new_field_should_render = true;
});

formState?.patchValue({
  "new_field_name": new_value,
});

...

// under build
if (new_field_should_render)
  FormBuilderField(
    name: "new_field_name"
  ),

This will not work as the field has not yet been registered. The workaround is to either use Offstage to make the field always stays inside the widget tree, or to call patchValue a frame later using addPostFrameCallback

However, debugging this is not easy.

I suggest we can add a warning log when patchValue cannot find the respective field using the key inside the map, warning the developer that the field with the given key is not registered and therefore the update to that field is ignored.

Alternatives you've considered

No response

Aditional information

No response

@Camerash Camerash added the enhancement New feature or request label Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant