首页 文章

颤振 - 下拉列表中的默认值

提问于
浏览
1

我正在尝试将元素发送到一个视图 . 此元素将是第二页下拉列表中的默认元素 . 我在第一页使用类似的东西

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

然后在第二页中,我创建了future以获取dropdown中的所有元素,并使用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,descripcion:Terreno Rio},{id:2,descripcion:Terreno Asier}]

下拉列表看起来与此类似

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(),
                    ),
                  ),

显示的错误是

value == null || items.where((DropdownMenuItem item)=> item.value == value) . length == 1':不是真的

显然代码很好但是当我转到第二个视图时显示1秒钟左右的错误视图然后显示第二页 . 我认为值可能加载太快而且下拉不能正确设置 . 知道为什么会发生这种错误,我该怎么办呢?

2 回答

  • 0

    's because at the first time you don' t有任何值(您的列表为空),因此您可以显示 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()),
    
  • 1

    可能有两件事情错了,我可以用你的代码来思考:

    • DropdownMenutItem项的值不是唯一的,即 . 一些"id"重复 .

    • 您传递给PageTwo的ID与菜单中的任何ID都不匹配 .

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

相关问题