Challenge - 5 Problems
DropdownButton Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
DropdownButton initial selected value behavior
What will be the initial selected value shown in this DropdownButton widget when the app starts?
Flutter
String dropdownValue = 'One'; DropdownButton<String>( value: dropdownValue, items: <String>['One', 'Two', 'Three'].map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), onChanged: (String? newValue) { dropdownValue = newValue!; }, )
Attempts:
2 left
💡 Hint
Look at the value property of DropdownButton and the initial value of dropdownValue.
✗ Incorrect
The DropdownButton's value property is set to 'One', so it shows 'One' as the selected item initially. The onChanged callback updates the variable but without setState, UI won't update on change, but initial display is correct.
📝 Syntax
intermediate2:00remaining
Correct syntax for DropdownMenuItem list
Which option correctly creates a list of DropdownMenuItem for the DropdownButton?
Attempts:
2 left
💡 Hint
Check the type annotations and constructor usage carefully.
✗ Incorrect
Option B correctly specifies the map generic type and uses the DropdownMenuItem constructor with typed value and child. Other options miss type annotations or constructor generics.
❓ lifecycle
advanced2:00remaining
Why DropdownButton does not update UI on selection?
Given this code snippet, why does the DropdownButton not update the selected value on user selection?
Flutter
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { String dropdownValue = 'One'; @override Widget build(BuildContext context) { return DropdownButton<String>( value: dropdownValue, items: <String>['One', 'Two', 'Three'].map((String val) { return DropdownMenuItem<String>(value: val, child: Text(val)); }).toList(), onChanged: (String? newVal) { dropdownValue = newVal!; }, ); } }
Attempts:
2 left
💡 Hint
Think about how Flutter knows to update the UI when state changes.
✗ Incorrect
Flutter rebuilds widgets when setState is called. Here, dropdownValue changes but setState is not called, so UI does not update.
advanced
2:00remaining
DropdownButton inside a form with validation
How can you ensure the DropdownButton's selected value is validated as required inside a Flutter Form widget?
Flutter
Form( key: _formKey, child: DropdownButtonFormField<String>( value: _selectedValue, items: _items, onChanged: (val) => setState(() => _selectedValue = val!), validator: (val) => val == null ? 'Please select an option' : null, ), )
Attempts:
2 left
💡 Hint
Flutter provides a special widget for dropdowns inside forms.
✗ Incorrect
DropdownButtonFormField integrates with Form and supports validator for required selection validation.
🔧 Debug
expert2:00remaining
Identify the runtime error in DropdownButton usage
What runtime error will this DropdownButton code cause when the app runs?
Flutter
String? dropdownValue; DropdownButton<String>( value: dropdownValue, items: <String>['Red', 'Green', 'Blue'].map((String val) { return DropdownMenuItem<String>(value: val, child: Text(val)); }).toList(), onChanged: (String? newVal) { dropdownValue = newVal!; }, )
Attempts:
2 left
💡 Hint
DropdownButton allows value to be null, which shows no selection initially.
✗ Incorrect
No runtime error occurs because the value property of DropdownButton is T? (nullable). When value is null, the dropdown displays no selected item initially, which is valid behavior. A runtime error only occurs if value is non-null and does not match any item's value.