0
0
Fluttermobile~20 mins

DropdownButton in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DropdownButton Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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!;
  },
)
AThe dropdown shows 'One' as selected initially.
BThe dropdown shows 'Two' as selected initially.
CThe dropdown shows no value selected initially.
DThe dropdown throws a runtime error because onChanged does not call setState.
Attempts:
2 left
💡 Hint
Look at the value property of DropdownButton and the initial value of dropdownValue.
📝 Syntax
intermediate
2:00remaining
Correct syntax for DropdownMenuItem list
Which option correctly creates a list of DropdownMenuItem for the DropdownButton?
A<String>['A', 'B', 'C'].map((val) => DropdownMenuItem<String>(value: val, child: Text(val))).toList()
B<String>['A', 'B', 'C'].map<DropdownMenuItem<String>>((String val) => DropdownMenuItem<String>(value: val, child: Text(val))).toList()
C<String>['A', 'B', 'C'].map((String val) => DropdownMenuItem(value: val, child: Text(val))).toList()
D<String>['A', 'B', 'C'].map<DropdownMenuItem<String>>((val) => DropdownMenuItem(value: val, child: Text(val))).toList()
Attempts:
2 left
💡 Hint
Check the type annotations and constructor usage carefully.
lifecycle
advanced
2: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!;
      },
    );
  }
}
ABecause the items list is not wrapped in a StatefulWidget.
BBecause the DropdownButton value is not nullable and causes a runtime error.
CBecause the dropdownValue variable is declared inside the build method.
DBecause the onChanged callback does not call setState to notify Flutter to rebuild the widget.
Attempts:
2 left
💡 Hint
Think about how Flutter knows to update the UI when state changes.
navigation
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,
  ),
)
AUse DropdownButtonFormField with a validator function to check if value is null.
BUse DropdownButton with onChanged and manually check value before form submission.
CWrap DropdownButton in a TextFormField to get validation features.
DUse a separate Checkbox to validate selection instead of DropdownButton.
Attempts:
2 left
💡 Hint
Flutter provides a special widget for dropdowns inside forms.
🔧 Debug
expert
2: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!;
  },
)
AA type mismatch error because dropdownValue is nullable but value expects non-null.
BA 'value' must be one of the items' values error because dropdownValue is null but value is non-nullable.
CNo error, the dropdown shows no selection initially.
DA null pointer exception because onChanged assigns null to dropdownValue.
Attempts:
2 left
💡 Hint
DropdownButton allows value to be null, which shows no selection initially.