I have a drop down menu in my application inside a custom flushbar(which acts like a snackbar). The flushbar pops up everytime the app is ran until the user dismisses it. To use the flushbar in my app, I have used an async function and called the function inside future in initState() - without doing this my snackbar or flushbar was not showing, please check below code:
@override
void initState() {
super.initState();
Future(() {
withInputField(context);
});
withInputField(BuildContext context) async {...}
So, the problem I am facing is the dropdownmenu is not working as it's supposed to. After I select an item, I would like the item to replace the hint text in the dropdown button. It is working fine, if I put the menu inside widget build scaffold but when I put the same code inside withInputField(..) function, the selected item is null. Below is my dropdown code:
class Company {
final int id;
final String name;
Company(this.id, this.name);
}
Company _selectedCompany;
List<Company> getCompany = <Company>[
Company(1, 'Apple'),
Company(2, 'Google'),
Company(3, 'Samsung'),
Company(4, 'Sony'),
Company(5, 'LG'),
];
The code below is inside the async function withInputField.
DropdownButton<Company>(
hint: Text('Select company'),
value: _selectedCompany,
onChanged: (Company val) {
setState(() {
_selectedCompany = val;
});
},
items: getCompany.map((Company company) {
return DropdownMenuItem<Company>(
value: company,
child: Text(company.name),
);
}).toList(),
),
I have tried many solutions from stackoverflow and followed various tutorials but nothing seems to work inside the async function. An answer with a good explanation is deeply appreciated as i would like to know why it is working in Widget build(context) but not inside the async function as mentioned above.
question from:
https://stackoverflow.com/questions/65936205/flutter-dropdownmenu-not-showing-selected-item 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…