Dropdown selection not displaying with Flutter

后端 未结 8 2057
攒了一身酷
攒了一身酷 2021-01-12 08:56

I\' m gettings items from JSON and displaying them in a dropdown. When the person select an item from the dropdown list, I get the selection but the selected item doesn\'t c

相关标签:
8条回答
  • 2021-01-12 09:45

    this was a bug reported in the Github of Flutter.

    The correct implementions is:

    class _DropdownButtonBugState extends State<DropdownButtonBug> {
    
      final List<String> _items = ['One', 'Two', 'Three', 'Four'].toList();
    
      String _selection;
    
      @override
      void initState() {
        _selection = _items.first;
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        final dropdownMenuOptions = _items
          .map((String item) =>
            new DropdownMenuItem<String>(value: item, child: new Text(item))
          )
          .toList();
    
        return new Scaffold(
          body: new DropdownButton<String>(
            value: _selection,
            items: dropdownMenuOptions,
            onChanged: (s) {
              setState(() {
                _selection = s;
              });
            }
          )
        );
      }
    }
    
    0 讨论(0)
  • 2021-01-12 09:49

    The Dropdown Button can be wrapped inside a StatefulBuilder() widget if the selected values are not updated after the selction.

    0 讨论(0)
提交回复
热议问题