如何在Flutter的Dropdown中实现数组

时间:2019-05-15 13:46:12

标签: arrays string flutter dropdown

我正在从服务器获取逗号分隔的字符串。然后,我使用.split()将其转换为数组。我必须将该数组填充到flutter的下拉列表中。怎么做。这是我的代码。

Widget _visitorPurpose()  {
SharedPref.sharedPreferenceGet(StringsConstants.CLIENT_PURPOSE).then((dynamic res){
    purposeTemp = res;
    print(res);
    purposeTemp2 = purposeTemp.split(",");
});    return StreamBuilder(
  // stream: bloc.email,
  builder: (context, snapshot) {

    return new Container(
      padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          new DropdownButton<String>(
            // value: selectedPurpose,
            hint: new Text(
              'Select visitng purpose',
              style: TextStyle(fontFamily: "Gotham"),
            ),
            items: <String>[purposeTemp]
                .map((purposeTemp) {
              return new DropdownMenuItem<String>(
                value: purposeTemp,
                child: new Text(
                  purposeTemp,
                  style: TextStyle(fontFamily: "Gotham"),

                ),

              );
            }).toList(),
            onChanged: (String purpose) {
              setState(() {
                selectedPurpose = purpose;
              });
            },
            // value: selectedPurpose,
          )
        ],
      ),
    );
  },
);

}

这是我在响应后转换数组的方式。 (结果)是我得到的答复。

result.config.values.elementAt(19).value

这是我的变量的定义方式。

 String purposeTemp  = "";
 var purposeTemp2 = [];

1 个答案:

答案 0 :(得分:0)

得到答案。对于其他参考,这是我的答案。

Widget _visitorPurpose()  {
SharedPref.sharedPreferenceGet(StringsConstants.CLIENT_PURPOSE).then((dynamic res){
    purposeTemp = res;
    print(res);
    purposeTemp2 = purposeTemp.split(",");
});    return StreamBuilder(
  // stream: bloc.email,
  builder: (context, snapshot) {

    return Expanded( 
    child:new Container(
      padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          new DropdownButton<String>(
            // value: selectedPurpose,
            hint: new Text(
              'Select visitng purpose',
              style: TextStyle(fontFamily: "Gotham"),
            ),
            items: purposeTemp2
                .map((purposeTemp) {
              return new DropdownMenuItem<String>(
                value: purposeTemp,
                child: new Text(
                  purposeTemp,
                  style: TextStyle(fontFamily: "Gotham"),

                ),

              );
            }).toList(),
            onChanged: (String purpose) {
              setState(() {
                selectedPurpose = purpose;
              });
            },
            // value: selectedPurpose,
          )
        ],
      ),
    ),
);