简体   繁体   English

Flutter DropdownButton 条件不起作用

[英]Flutter DropdownButton conditions not working

I have a list of values meant to be parsed into a dropdown button with JSON from a rest API.我有一个值列表,要从 rest API 中解析为带有 JSON 的下拉按钮。 Values are parsed fine and well but I applied a condition that when there is no list of values available to display then it should simply show a single text on the dropdown saying "no equipment".值被很好地解析,但我应用了一个条件,即当没有可显示的值列表时,它应该在下拉列表中简单地显示一个文本,说“没有设备”。 But for some reason, this is not working.但由于某种原因,这不起作用。

My code:我的代码:

List data = List();
Future<String> getSWData() async {
    setState(() {
      isLoading = true;
    });
        var res = await http
            .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
        resBody = json.decode(res.body);

        setState(() {
          data = resBody;
          isLoading = false;
        });


        return "Sucess";

    }
    child: DropdownButton(
                          hint: new Text(
                            "Select Equipment",
                            style: style,
                          ),
                          style: style,
                          onChanged: (newVal) {
                            setState(() {
                              _mySelectionEquipment = newVal;
                            });
                          },
                          value: _mySelectionEquipment,
                          items: data.map((item) {
                            return new DropdownMenuItem(
                              child: Row(
                                children: <Widget>[
                                  Text(item['EquipmentMake'] ??
                                      "No Equipment Registered"),
                                  SizedBox(
                                    width: 5,
                                  ),
                                  SizedBox(
                                    width: 5,
                                  ),
                                  Text(item['EquipmentModel'] ?? ""),
                                ],
                              ),
                              value: item['EquipmentID'].toString(),
                            );
                          }).toList(),

                          isExpanded: false,
                        ),

You call Map on empty List您在空列表上调用 Map

Try something like this:尝试这样的事情:

  items: data.length > 0
                                ? data.map((item) {
                                    return new DropdownMenuItem(
                                      child: Row(
                                        children: <Widget>[
                                          Text(item['EquipmentMake'] ??
                                              "No Equipment Registered"),
                                          SizedBox(
                                            width: 5,
                                          ),
                                          SizedBox(
                                            width: 5,
                                          ),
                                          Text(item['EquipmentModel'] ?? ""),
                                        ],
                                      ),
                                      value: item['EquipmentID'].toString(),
                                    );
                                  }).toList()
                                : [
                                    DropdownMenuItem(
                                        child: Row(
                                          children: <Widget>[
                                            Text("No Equipment Registered"),
                                            SizedBox(
                                              width: 5,
                                            ),
                                            SizedBox(
                                              width: 5,
                                            ),
                                            Text(""),
                                          ],
                                        ),
                                        value: 0.toString())
                                  ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM