Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

drop down menu - Flutter DropdownMenu not showing selected item

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Fixed the problem after I posted the question. Just used a StatefulBuilder inside the function withInputField as the Drop Down Menu is changing state after an element is clicked. As the snackbar is only built once, the widget was not changing its state. Please refer to this answer in stackoverflow https://stackoverflow.com/a/57240941/6067774.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...