Skip to content

form reset() function gives null pointer for a dropdownmenu with onChange=null #146335

@JarnoD81

Description

@JarnoD81

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

a: text inputEntering text in a text field or keyboard related problemsfound in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-text-inputOwned by Text Input team

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions