Ad
How To Add Json List Items To Dropdown Search Items In Flutter | Dart
I'm creating a dropdown button with search. So I using this package . Here with this package I have problem. The response from the API is like this.
List countries = [
{
'id': '1',
'name': 'Brazil',
},
{
'id': '2',
'name': 'India',
},
{
'id': '3',
'name': 'Japan',
},
{
'id': '4',
'name': 'Tokyo',
},
{
'id': '5',
'name': 'Australia',
},
{
'id': '6',
'name': 'Srilanka',
},
{
'id': '7',
'name': 'Canada',
},
];
My Dropdown code is this,
body: Column(
children: [
DropdownSearch<String>(
mode: Mode.MENU,
items: countries['name'],
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
onChanged: (value){
print(countries['name']);
},
selectedItem: "Canada",
),
],
),
Here items: countries['name']
line i getting error as The argument type 'String' can't be assigned to the parameter type 'int'.
Also when i select a country i want to print the country id. eg: if i select country as japan then it should print 4 in console. My Code is not working.
Ad
Answer
First of all, a List of Map doesn't work like this -> (countries['name]
), try replace your items
to:
DropdownSearch<String>(
mode: Mode.MENU,
items: countries.map((e)=>e['name']).toList(),
showSearchBox: true,
label: "Menu mode",
hint: "country in menu mode",
onChanged: (value){
print(value);
},
selectedItem: "Canada",
),
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad