I have a list of locations that i want to implement as a dropdown list in Flutter. Im pretty new to the language. Here's what i have done.
new DropdownButton(
value: _selectedLocation,
onChanged: (String newValue) {
setState(() {
_selectedLocation = newValue;
});
},
items: _locations.map((String location) {
return new DropdownMenuItem<String>(
child: new Text(location),
);
}).toList(),
This is my list of items:
List<String> _locations = ['A', 'B', 'C', 'D'];
And I am getting the following error.
Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 468 pos 15: 'value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.
I assume the value of _selectedLocation
is getting null. But i am initialising it like so.
String _selectedLocation = 'Please choose a location';
For the solution, scroll to the end of the answer.
First of all, let's investigate what the error says (I have cited the error that's thrown with Flutter 1.2, but the idea is the same):
There are four
or
conditions. At least one of them must be fulfilled:DropdownMenuItem
widgets) were provided. This eliminatesitems == null
.items.isEmpty
._selectedLocation
) was also given. This eliminatesvalue == null
. Note that this isDropdownButton
's value, notDropdownMenuItem
's value.Hence only the last check is left. It boils down to something like:
The way code is presented, there is not a
DropdownMenuItem
widget that has a value of_selectedLocation
. Instead, all the widgets have their value set tonull
. Sincenull != _selectedLocation
, last condition fails. Verify this by setting_selectedLocation
tonull
- the app should run.To fix the issue, we first need to set a value on each
DropdownMenuItem
(so that something could be passed toonChanged
callback):The app will still fail. This is because your list still does not contain
_selectedLocation
's value. You can make the app work in two ways:items.where((DropdownMenuItem<T> item) => item.value == value).length == 1
). Might be useful if you want to let the user re-selectPlease choose a location
option.hint:
paremter and setselectedLocation
tonull
(to satisfyvalue == null
condition). Useful if you don't wantPlease choose a location
to remain an option.See the code below that shows how to do it:
It had happened to me when I replace the default value with a new dynamic value. But, somehow your code may be dependent on that default value. So try keeping a constant with default value stored somewhere to fallback.
When I ran into this issue of wanting a less generic DropdownStringButton, I just created it:
dropdown_string_button.dart
Use
StatefulWidget
andsetState
to update dropdown.initial state of dropdown:
Open dropdown and select value:
Reflect selected value to dropdown:
I was facing a similar issue with the DropDownButton when i was trying to display a dynamic list of strings in the dropdown. I ended up creating a plugin : flutter_search_panel. Not a dropdown plugin, but you can display the items with the search functionality.
Use the following code for using the widget :
place the value inside the items.then it will work,