how do you map index value to text for dropdownButton - flutter

In flutter I how do you map index to string. Example I have a List for numbers. I want to display text and capture the value in the back ground that would be numbers. I am using Dropdown menu item but you can only pass one value.
final List nums = ['1', '2', '3','4','5', '6',]
DropdownButton(
items: nums.map((e) {
return DropdownMenuItem(
value: e,
child: Text(e));
}).toList(),
);
I want to do something like:
final List nums = ['1': 'one', '2': 'two', '3': 'three','4': 'four','5': 'five', '6': 'six'];
i represent the number
e represent the text
DropdownButton(
items: nums.map((i, e) {
return DropdownMenuItem(
value: i,
child: Text(e));
}).toList(),
);

You could use the dropdown as here :
final Map nums = ['1': 'one', '2': 'two', '3': 'three','4': 'four','5': 'five', '6': 'six'];
String dropdownValue = nums["one"]; //set the dropdown's default value
DropdownButton(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = nums[newValue]; //update the dropdown's value
});
},
items: nums.map((i, e) {
return DropdownMenuItem(
value: i,
child: Text(e));
}).toList(),
);

To capture the index or incremental value for the the string value. You should use indexof. Here is an example:
final List nums = ['one', 'two', 'three','four', 'five', 'six'];
int dropdownValue;
DropdownButton(
onChanged: (String newValue) {
setState(() {
dropdownValue = nums.indexOf(newValue) + 1;
});
},
items: nums.map((e) {
return DropdownMenuItem(
value: i,
child: Text(e));
}).toList(),
);

Related

Dropdownbutton state management, Flutter

I coded a dropdownbutton so it gets the items from a database from back4app, it shows the user the items that he saved on said database. But when I try to hold the state for the selection of said item, it shows a blank dropdownbutton, eventhough it is showing the items from the database. Here is the error: [A value of type 'String' can't be assigned to a variable of type 'List'.
Try changing the type of the variable, or casting the right-hand type to 'List']. What did I do wrong?
DropdownButton<String>(
items: dropdownItems.map((ParseObject value) {
return DropdownMenuItem<String>(
value: value.get<String>('numeracao')!,
child: Text(value.get<String>('numeracao')!),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownItems = newValue!;
});
},
),
You need a new var to hold the current value. Let's say String dropdownValue. Then your DropownButton should look something like this:
DropdownButton<String>(
value: dropdownValue,
items: dropdownItems.map((ParseObject value) {
return DropdownMenuItem<String>(
value: value.get<String>('numeracao')!,
child: Text(value.get<String>('numeracao')!),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
),

In multiple dropdown menu, how to disable an option if already selected in Flutter?

I have 4 different dropdown fields with 5 options and I want to disable/remove an option from other fields if the option is already selected.
Screenshot:
Screenshot 1
Screenshot 2
DropDown Menu Code:
String opemo1, opemo2, opemo3, opemo4;
List<String> emoji = [
"❤️",
"🤩",
"✌️",
"😂",
"😡",
];
DropdownButtonFormField(
validator: (value) =>
value == null ? 'required' : null,
hint: Text('❤️'),
value: opemo1,
icon: Icon(Icons.arrow_drop_down),
iconSize: 36,
isExpanded: true,
style: TextType.regularDarkText,
onChanged: (newValue) {
setState(() {
opemo1 = newValue;
pollDataController.setop1Emoji(newValue);
});
},
items: emoji.map((opemo1) {
return DropdownMenuItem(
value: opemo1,
child: Text(opemo1),
);
}).toList(),
),
To manage it you need to remove the selected emoji from List<String> emoji.
onChanged: (newValue) {
setState(() {
emoji.removeWhere((element) => element == newValue); /// This removed selected emoji.
opemo1 = newValue;
pollDataController.setop1Emoji(newValue);
});
},
You can replace your onChanged with this snippet.

Flutter function error: Column's children must not contain any null values, but a null value was found at index 1

After I separate a dropdown list to a separate method in flutter, the debugger returns the following error:
"Column's children must not contain any null values, but a null value was found at index 1"
This is the code I had to a separate method _actionDropdown():
_actionDropdown() {
DropdownButton<String>(
value: actionValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.deepPurple
),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
// if(dropdownValue == 'Move to...') {
// return Text('add chips for folders here');
// } else if(dropdownValue == 'Label as...') {
// return Text('add chips for labels here');
// }
});
},
items: <String>['Archive', 'Delete', 'Move To...', 'Label as...']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
);
}
This chunk of code for DropdownButton<String>works as a column child but not when I add the separated method _actionDropdownas a child. What am I missing?
As #brendan suggested you forgot to add return keyword.
_actionDropdown() {
return DropdownButton<String>( // return added here
value: actionValue,

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
")