Dropdown in Flutter Example: DropdownButton Done Right
The dropdown is one of those widgets that looks trivial in a tutorial and gets awkward in a real app: a nullable value, a list that changes after a network call, a form that will not validate, or a menu that overflows on a small phone. This walkthrough covers DropdownButton and DropdownButtonFormField from the ground up, then adds the pieces production screens actually need.
You will see a complete working example, how to keep the selected value in state without rebuild bugs, how to wire validation into a Form, and the handful of mistakes that account for most dropdown bugs in Flutter codebases.
DropdownButton vs DropdownButtonFormField
Both widgets render the same Material menu, but they differ in what surrounds it. DropdownButton is a plain widget: you own the selected value, you pass onChanged, and you decide how it looks. DropdownButtonFormField wraps that behaviour in a FormField, so it participates in Form validation, shows error text under the field, and resets with a Form key.
Rule of thumb: if the dropdown lives inside a Form with a save button, use DropdownButtonFormField. If it is a filter, a toolbar control, or part of a custom layout, DropdownButton is lighter and gives you full control over the decoration.
A subtle difference matters for state: DropdownButtonFormField keeps its own internal value once built, so if your list of options changes you must pass a key or rebuild deliberately, otherwise the field can hold a value that is no longer in the items list and throw.
- DropdownButton: manual value, manual error display
- DropdownButtonFormField: validation, error text, Form reset
- Both share the same items and onChanged API
- Changing the items list means checking the current value
A Complete Working Example
Start with a typed model rather than raw strings. Define a small class with an id and a label, or use an enum if the set is fixed. Typed values make onChanged null-safe and prevent the classic bug of comparing strings that differ by whitespace or case.
The build method holds a nullable selected value, items built with map from your list, and an onChanged that calls setState. Keep the generic type explicit, for example DropdownButton<String>, so the compiler catches mismatches between value and items early.
If you want a hint before a choice is made, set value to null and provide a hint widget. Setting value to an item that is not present in items throws at runtime, so guard the initial value against your data source.
- Use a typed value (enum or model), not loose strings
- Keep selected value as nullable state
- Set hint for the empty state instead of a fake first item
- Never set value to something missing from items
| Approach | Best for | Validation | Searchable | Effort |
|---|---|---|---|---|
| DropdownButton | Filters, toolbars, custom layouts | Manual | No | Low |
| DropdownButtonFormField | Forms with save and validation | Built-in | No | Low |
| Searchable picker route | 20+ options, country or city lists | Manual or shared | Yes | Medium |
| Segmented control | 2-3 mutually exclusive choices | N/A | No | Low |
| Modal bottom sheet list | Mobile-first selection with icons | Manual | Optional | Medium |
Handling State and Async Data
Most real dropdowns are filled from a repository call. Show a disabled dropdown or a loading placeholder until data arrives, then build items from the loaded list. If your default selection depends on that data, set it in the same setState that stores the list so the widget never builds with a value it cannot find.
When the list can change later, for example after a refresh, reconcile the current selection: if the previously selected id is still present keep it, otherwise fall back to null or the first item. Doing this in one place keeps the UI predictable.
For forms with many fields, keep the selected values in a single state object rather than many separate variables. That also makes it easier to persist input across configuration changes, as described in Handling Configuration Changes in Kotlin Without Losing State for the Android side of the same problem.
- Load options first, then set value in the same setState
- Reconcile selection after every list refresh
- Disable the dropdown while loading
- Group related selections in one state object
Validation, Search, and Long Lists
DropdownButtonFormField accepts a validator that returns a string to show as an error. Return null when valid. Call validate on the Form key when the user taps save, and the error text appears under the field with no extra work. This is far less code than manually tracking an error string for a plain DropdownButton.
The built-in menu is not searchable. Past roughly 15 to 20 options users start scrolling and hunting, so switch to a searchable picker: open a full-screen or modal route with a TextField and a filtered ListView. Keep the same typed value so the rest of your form does not change.
On very small screens, a long menu can overflow vertically. Setting isExpanded to true lets the button fill its parent width and avoids text overflow on long labels; menuMaxHeight caps the open menu so it does not cover the whole screen.
- Use validator for required-field rules
- Call formKey.currentState.validate on submit
- Switch to a searchable picker above ~20 options
- Set isExpanded and menuMaxHeight for tight layouts
Styling and Accessibility
DropdownButton exposes itemHeight, icon, iconSize, underline, and a style for the selected text. Wrap it in an InputDecorator or use DropdownButtonFormField with a decoration to get a border, label, and padding that match your TextFields, which is the quickest way to make a form look consistent.
Each item should have enough height for touch, generally around 48 logical pixels, which matches the default itemHeight. If you shrink it for density, keep the tap target comfortable and make sure the label still fits at large text scale settings.
For accessibility, the selected item is announced, but give meaningful labels: use the same string in the item child and its value display so screen readers read the visible text. Avoid icon-only items without a text label.
- Match decoration to your TextField styling
- Keep items near 48 logical pixels tall
- Test with large system font scale
- Always show a text label per item
Common Mistakes to Avoid
The most frequent crash is value not matching any item. It happens when a stored id is compared to a rebuilt list, or when a null check is missing. Add an assertion or a small helper that returns null when the value is absent, and the exception disappears.
Second is forgetting that onChanged receives null for the hint or when the user cannot deselect in most cases, so treat the parameter as nullable. Third is using a dropdown for two or three boolean options where a segmented control or switch is clearer and faster to tap.
Finally, do not rebuild items inside build with new object instances on every frame if you compare by identity. Either use value equality or compare by id; otherwise the selected check fails silently and the field appears blank.
- Guard against value not in items
- Treat onChanged as nullable
- Prefer segmented controls for 2-3 options
- Compare by id or use value equality
Key points
- Use DropdownButtonFormField inside forms, DropdownButton for filters and custom layouts.
- Keep the selected value typed and nullable, and never set it to an item missing from items.
- Switch to a searchable picker once options pass roughly 20.
- Reconcile the selection whenever the options list is refreshed from async data.
Flutter Screen Widget and Cost Estimator
Estimate the number of widgets in a screen and a rough development-hour figure from your component counts. Adjust the fields to see how each UI area affects the total effort.
Editor and Flutter contributor
Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.