Dropdown selection not displaying with Flutter

后端 未结 8 2110
攒了一身酷
攒了一身酷 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 {
    
      final List _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(value: item, child: new Text(item))
          )
          .toList();
    
        return new Scaffold(
          body: new DropdownButton(
            value: _selection,
            items: dropdownMenuOptions,
            onChanged: (s) {
              setState(() {
                _selection = s;
              });
            }
          )
        );
      }
    }
    

提交回复
热议问题