DropDownButton items in a variable - flutter

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.

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!;
});
},
),

Can I get multiple return from dropdownbutton?

I tried to make json to dropdownbutton today.
But I want to get 2 values(ID and Name both) from it.
this is my json
[{"StudentID":"3","StudentName":"Amy"},{"StudentID":"4","StudentName":"Derek"}]
and this is my code of dropdown button.
Row(
children: <Widget>[Container(
padding: EdgeInsets.only(left:5),
child: new DropdownButton(
value: _StudentSelection,
items: StudentData.map((product) {
return new DropdownMenuItem(
value: product["StudentID"].toString(),
child: new Text(product["StudentName"]!)
)
}).toList(),
onChanged: (String? newValue) {
setState(() {
_StudentSelection = newValue!;
});
},
hint: Text('StudentID'),
)
),
],
),
in this case variety _StudentSelection is already initialized by Amy and StudentData is result of decoding json.
Thank you for seeing this question :)
Make your _StudentSelection variable's type the same type as your product variable and then use product as a value:, not just the ID.
Row(
children: <Widget>[Container(
padding: EdgeInsets.only(left:5),
child: new DropdownButton(
value: _StudentSelection, // change this variables type to the type of your product variable
items: StudentData.map((product) {
return new DropdownMenuItem(
value: product, // use the whole product as value
child: new Text(product["StudentName"]!)
)
}).toList(),
onChanged: (TYPE_OF_PRODUCT_HERE? newValue) {
setState(() {
_StudentSelection = newValue!;
});
},
hint: Text('StudentID'),
)
),
],
),

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 do you map index value to text for dropdownButton

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(),
);

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