Repository navigation
form reset() function gives null pointer for a dropdownmenu with onChange=null #146335
Description
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.2.1 23C71 darwin-x64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.2)
[✓] Connected device (3 available)
[✓] Network resources
We have a DropdownButtonFormField which we have disabled by setting the onChanged to null. This works fine as the look and feel of the widget show a disabled state and the user cannot change the value. I just figured out that we can also set the items to null and use the disabledHint property so I am not sure if setting onChanged to null is a "valid" way of disabling a DropdownButtonFormField.
Once the user is done submitting the form we reset it using a GlobalKey
final _formKey = GlobalKey(debugLabel: '_ClientEditState');
and call the reset() function on the formState like so:
_formKey.currentState?.reset()
This triggers a reset() on all form fields but on a DropdownButtonFormField without a onChanged it creates a NPE.
This is because of the following line of code in the dropdown reset() function:
_dropdownButtonFormField.onChanged!(value);
As onChanged can be null (and it seems also that this can be intended behaviour to disable the widget because the widget becomes disabled) there should be a null check around this call.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone (PR merged)
Activity
bleroux commentedon Apr 5, 2024
Thanks for the report.
Code sample for repro
Fix DropdownButtonFormField throws when onChange is null (#146342)
Fix DropdownButtonFormField throws when onChange is null (flutter#146342
github-actions commentedon Apr 22, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.Fix DropdownButtonFormField throws when onChange is null (#146342)