Flutter populate dropdownmenu with JSON

前端 未结 1 1881
难免孤独
难免孤独 2020-12-11 10:35

I am trying to take JSON returned from rest api and populate a dropdownmenu, I need to have the value different from the child value. Has anyone done this or have an exampl

相关标签:
1条回答
  • 2020-12-11 11:11

    You are getting this error because you are initializing _referPractice with a value that !null, and you are feeding it to the property value of the DropDownButton, which represents the currently selected item, and has to be null if no item has been selected yet.

    I have replicated your example using the JSON you provided:

    String _mySelection;
      List<Map> _myJson = [{"id":0,"name":"<New>"},{"id":1,"name":"Test Practice"}];
    
      @override
      Widget build(BuildContext context) {
         return new Scaffold(
            body: new Center(
              child: new DropdownButton<String>(
                isDense: true,
                hint: new Text("Select"),
                value: _mySelection,
                onChanged: (String newValue) {
    
                  setState(() {
                    _mySelection = newValue;
                  });
    
                  print (_mySelection);
                },
                items: _myJson.map((Map map) {
                  return new DropdownMenuItem<String>(
                    value: map["id"].toString(),
                    child: new Text(
                      map["name"],
                    ),
                  );
                }).toList(),
              ),
            ),
          );
    
      }
    
    0 讨论(0)
提交回复
热议问题