【问题标题】:Flutter - Default value in dropdownFlutter - 下拉列表中的默认值
【发布时间】:2018-11-30 10:11:07
【问题描述】:

我正在尝试将一个元素发送到一个视图。该元素将成为第二页下拉菜单中的默认元素。我在第一页使用类似的东西

  onTap: () {
                        Navigator.push(
                            context,
                            new MaterialPageRoute(
                                builder: (context) => new SummaryPage(,
                                    dropdownItem: dropdownSelection)));
                      } 

然后在第二页中,我创建 future 以获取下拉列表中的所有元素,并使用 initstate 方法设置默认元素下拉列表

 List data = List();

 Future<Null> getEstates() async {
    final response = await http
        .get('URL'
    }).catchError((error) {
      print(error.toString());
    });
    final responseJson = json.decode(response.body);
    final getBody = responseJson['body'];
    setState(() {
      data = getBody;
      loading = false;
    });
  }


 void initState() {
    super.initState();     

     setState(() {
          dropdownSelection = widget.dropdownItem.toString();
        });

getEstate 返回这个

[{id: 1, 描述: Terreno Rio}, {id: 2, 描述: Terreno 阿西尔}]

下拉菜单看起来像这样

child: ButtonTheme(
                    alignedDropdown: true,
                    child: new DropdownButton<String>(
                      isDense: true,
                      hint: new Text("Search...",
                          textAlign: TextAlign.center),
                      value: dropdownSelection,
                      onChanged: (String newValue) {
                        setState(() {
                          dropdownSelection = newValue;
                      },
                      items: data.map((item) {
                        return new DropdownMenuItem<String>(
                          child: new Text(
                            item['description'],
                          ),
                          value: item['id'].toString(),
                        );
                      }).toList(),
                    ),
                  ),

显示的错误是

值 == 空值 || items.where((DropdownMenuItem item) => item.value == value).length == 1': 不正确

显然代码很好,但是当我转到第二个视图时,会在 1 秒左右显示错误视图,然后显示第二页。我认为该值可能加载得太快并且下拉菜单无法正确设置。知道为什么会发生此错误以及如何解决它们吗?

【问题讨论】:

    标签: dart flutter dropdown


    【解决方案1】:

    那是因为第一次你没有任何值(你的列表是空的),所以你可以显示一个CircleProgressIndicator,像这样:

            child: data.length > 0 ? ButtonTheme(
                            alignedDropdown: true,
                            child: new DropdownButton<String>(
                              isDense: true,
                              hint: new Text("Buscar..",
                                  textAlign: TextAlign.center),
                              value: dropdownSelection,
                              onChanged: (String newValue) {
                                setState(() {
                                  dropdownSelection = newValue;              
                              },
                              items: data.map((item) {
                                return new DropdownMenuItem<String>(
                                  child: new Text(
                                    'Finca: ' + item['descripcion'],
                                  ),
                                  value: item['id'].toString(),
                                );
                              }).toList(),
                            ),
                          ),
                        ) : Center(child: CircularProgressIndicator()),
    

    【讨论】:

      【解决方案2】:

      可能有两件事错了,我可以用你的代码想到:

      1. DropdownMenutItem 项的值不是唯一的,即。一些“id”是重复的。
      2. 您传递给 PageTwo 的 ID 与菜单中的任何 ID 都不匹配。

      我建议只使用 int id 而不是 String 作为值

      【讨论】:

      • 我找不到这个错误。如果我通过 dropdownSelection = "1" : items.where((DropdownMenuItem item) => item.value == value).length == 1': 不正确。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 2013-10-23
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      相关资源
      最近更新 更多