Flutter-下拉菜单中的默认值

时间:2018-11-30 10:11:07

标签: dart flutter dropdown

我正在尝试将元素发送到一个视图。该元素将是第二页下拉菜单中的默认元素。我在首页中使用了类似的方法

  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   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   ==值)。长度== 1':不正确

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

2 个答案:

答案 0 :(得分: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()),

答案 1 :(得分:0)

可能有两件事是错误的,我可以用您的代码来思考:

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

我建议仅使用int id代替String作为值

相关问题