Skip to content

DropdownMenuFormField invokes onSelected twice #173977

@StephenWithPH

Description

@StephenWithPH

Steps to reproduce

In DropdownMenuFormField's constructor, we invoke super (which is of type FormField) with a builder that declares the onSelectedHandler function, closing over _DropdownMenuFormFieldState<T>'s didChange:

final _DropdownMenuFormFieldState<T> state = field as _DropdownMenuFormFieldState<T>;
void onSelectedHandler(T? value) {
field.didChange(value);
onSelected?.call(value);
}

... and later uses it in the constructor of DropdownMenu:

For reference, here is the declaration (on _DropdownMenuFormFieldState) of didChange:

@override
void didChange(T? value) {
super.didChange(value);
_dropdownMenuFormField.onSelected?.call(value);
_updateRestorableController(value);
}

Note that didChange invokes _dropdownMenuFormField.onSelected?.call(value). Note that onSelectedHandler (linked above), first invokes didChange, then itself invokes onSelected?.call(value).

As a result, the original final ValueChanged<T?>? onSelected; passed in via constructor argument below to DropdownMenuFormField is double-invoked.

Expected results

onSelected is invoked once when a DropdownMenuEntry is selected within a DropdownMenuFormField.

Actual results

onSelected is invoked twice when a DropdownMenuEntry is selected within a DropdownMenuFormField.

Code sample

N/A

Screenshots or Video

N/A

Logs

N/A

Flutter Doctor output

[!] Flutter (Channel stable, 3.35.1, on macOS 15.6 24G84 darwin-arm64, locale en-US)

Metadata

Metadata

Labels

P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.found in release: 3.33Found to occur in 3.33found in release: 3.35Found to occur in 3.35frameworkflutter/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-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions