Create a Dropdown Button from a list of maps - flutter

I'm trying to create a dropdown button using a list of maps.
This is my list which is in _location['category']:
[{id: 12345, title: My category}]
And the DropdownButton:
DropdownButton(
items: _location['category'].map((item) {
return DropdownMenuItem(
child: Text(item['title']),
value: item.id,
);
}),
onChanged: (newVal) => print(newVal),
value: null,
),
Error: The following _TypeError was thrown building LocationDialog(dirty, state: _LocationDialogState#6947a):
type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<DropdownMenuItem<Null>>'

Add .toList() to make your .map a List.
The code below should help
Create a List called category somewhere then use it with the DropdownButton
List category = _location['category'];
DropdownButton(
items: category.map((item) {
return DropdownMenuItem(
child: Text(item['title']),
value: item.id,
);
}).toList(),
onChanged: (newVal) => print(newVal),
value: null,
),

Related

Searchable Dropdown search is not working when i access the id as value inside DropdownMenuItem | Flutter

Iam using searchable_dropdown: ^1.1.3 package to search elements in dropdownmenu, when i use district (which give district name) for the value inside DropdownMenuItem it works properly. But the problem is when a user selects district i want the value as districtId. but when i give districtId as value the search is not working. Below is my code.
Center(
child: SearchableDropdown.single(
isExpanded: true,
value: selectedName,
hint: "Select one",
searchHint: "Select one",
// hint: Text('Select Name'),
items: districtList.map(
(list) {
return DropdownMenuItem(
child: Text(list['district']),
value: list['districtId'],
);
},
).toList(),
onChanged: (value) {
setState(() {
selectedDistrict = value;
print(selectedDistrict );
});
},
),
),
This is the package https://pub.dev/packages/searchable_dropdown

Drop down button in flutter: value isn't compiling

I have this drop down button in flutter:
DropdownButton(
value: _value,
items: [
DropdownMenuItem(
child: Text("First Item"),
value: 1,
),
DropdownMenuItem(
child: Text("Second Item"),
value: 2,
),
DropdownMenuItem(
child: Text("Third Item"),
value: 3
),
DropdownMenuItem(
child: Text("Fourth Item"),
value: 4
)
],
onChanged: (value) {
setState(() {
_value = value; //here I get the error
});
})
I get the following error: A value of type 'Object?' can't be assigned to a variable of type 'int'.
On the following line: _value = value;
Any help is appreciated.
You need to specify the type of value with the generic on the DropdownButton constructor:
DropdownButton<int>(
...
The way you currently have it, the DropdownButton does not know what type value is supposed to be, so the onChanged callback provides the most generic type, an Object?. Adding the generic parameter tells the button that it should expect and emit int values.

How to use custom data types dropdownlist buttons in flutter?

I want to make drop-downlistbutton but its list should be elements of enum now I tried to change the type but it showed an error now I know I could do with strings and later on use if-else to do but obviously would make the code long so is there any way like to change the string to datatype of an enum.
Yes, you can easily do that. Just specify the data type you want to use, here we use DropdownButton<CustomType>
enum CustomType { TYPE1, TYPE2, TYPE3 }
DropdownButton<CustomType>(
onChanged: (value) => print(value),
items: [
DropdownMenuItem(
child: Text("TYPE 1"),
value: CustomType.TYPE1,
),
DropdownMenuItem(
child: Text("TYPE 2"),
value: CustomType.TYPE2,
),
DropdownMenuItem(
child: Text("TYPE 3"),
value: CustomType.TYPE3,
),
],
)
Iterating over enum values
DropdownButton<CustomType>(
onChanged: (value) => print(value),
items: CustomType.values
.map((type) => DropdownMenuItem(
child: Text(type.toString().split('.')[1]),
value: type,
))
.toList(),
)
I hope this is what you are looking for.

DropDownButton items in a variable

I'm trying to store the possible values for a DropDownButton in a variable.
Goal:
On the press of a button, I want a new list of Strings to be created and assigned as possible choices for the DropdownButton. To do so, I need to store the possible choices (list of DropdownMenuItem) in a variable.
This sample of code works:
static String defaultDropDownValue = 'Select your Network';
String dropdownValue = defaultDropDownValue;
[...]
new Flexible(
child: DropdownButton(
isExpanded: true,
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
_checkSsid();
},
items: <String>[defaultDropDownValue, 'One', 'Two', 'Three', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
[...]
Now, this sample of code does not work:
static String defaultDropDownValue = 'Select your Network';
String dropdownValue = defaultDropDownValue;
List<DropdownMenuItem> dropdownList = [defaultDropDownValue, 'one', 'two'].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList();
[...]
new Flexible(
child: DropdownButton(
isExpanded: true,
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
_checkSsid();
},
items: dropdownList,
),
),
[...]
The compiler returns the following error message:
Error: The argument type 'Null Function(String)' can't be assigned to
the parameter type 'void Function(dynamic)'. Try changing the type of
the parameter, or casting the argument to 'void Function(dynamic)'.
onChanged: (String newValue) {
I don't really understand the meaning of the message, nor its cause.
Becasue you don't explicitly define the Type for array in the second example,
You could try DropdownMenuItem<String> and DropdownButton<String> to explicit them.
static String defaultDropDownValue = 'Select your Network';
String dropdownValue = defaultDropDownValue;
List<DropdownMenuItem<String>> dropdownList = [defaultDropDownValue, 'one', 'two'].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList();
[...]
new Flexible(
child: DropdownButton<String>(
isExpanded: true,
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
_checkSsid();
},
items: dropdownList,
),
),
[...]
Referenced from DropdownButton's Documentation below:
The type T is the type of the value that each dropdown item represents. All the entries in a given menu must represent values with consistent types. Typically, an enum is used. Each DropdownMenuItem in items must be specialized with that same type argument.

flutter dropdown not storing select value

I have flutter dropdown select list:
as
ListTile(
leading: const Icon(Icons.local_shipping),
title: DropdownButton<String>(
value: serviceProvider,
// hint: const Text('Select Service Provider'),
onChanged: (String newValue) {
setState(() {
serviceProvider = newValue;
});
},
items: <Map>[{"id":"1","business_name":'Business Name 1'}, {"id":"3","business_name":'Business Name 3'}, {"id":"6","business_name":'Business Name 6'}, {"id":"5","business_name":'Business Name 5'}].map<DropdownMenuItem<String>>((Map value) {
return DropdownMenuItem<String>(
value: value['id'],
child: Text(value['business_name']),
);
}).toList(),
),
),
If i decleare String serviceProvider = "6"; it just work fine. but after selecting one item from list it doest not persist there.
I am not able to figure out what's missing in my code,
Thanks
")