如何在DropdownButton中显示重复值

时间:2020-01-28 07:44:05

标签: flutter flutter-layout

我正在从服务器获取json数据。在这里,我以品牌并显示为DropDownItems。当我选择重复值应用程序时崩溃。如果我选择唯一值,则效果很好。我在android和iOS上都进行了测试。我俩都面临着同样的问题。

下面是我的代码

Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 20.0, vertical: 0.0),
                      child: Container(
                        color: Colors.white,
                        child: Padding(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 10.0, vertical: 0.0),
                          child: DropdownButton(
                              isExpanded: true,
                              icon: Image.asset('images/drop_down_image.png'),
                              underline: SizedBox(),
                              items: vehicleModels.map((dropdownStringItem) {
                                return DropdownMenuItem(
                                  value: dropdownStringItem["brand"].toString(),
                                  child: Text(dropdownStringItem["brand"]),
                                );
                              }).toList(),
                              onChanged: (String selectedValue) {
                                setState(() {
                                  loadVariants(vehicelTypeSelected, vehicleBrandSelected, selectedVehicleModel);
                                  this.selectedVehicleModel = selectedValue;
                                });
                              },
                            hint: Text("Select Vehicle Model"),
                            value: selectedVehicleModel,
                          ),
                        ),
                      ),
                    ),

Json响应

[{
            "vehicleModelId": "22cd4acf-1b0f-11ea-98f1-27c4af3b3778",
            "brand": "Aveo [2006-2009]"
        },
        {
            "vehicleModelId": "22cd4ad0-1b0f-11ea-98f1-67942c5e5bd1",
            "brand": "Aveo [2006-2009]"
        },
        {
            "vehicleModelId": "22cd4ad1-1b0f-11ea-98f1-f360030e6b68",
            "brand": "Aveo [2006-2009]"
        },
        {
            "vehicleModelId": "22cd4ad2-1b0f-11ea-98f1-83212846a413",
            "brand": "Aveo [2006-2009]"
        },
        {
            "vehicleModelId": "22cd4ad9-1b0f-11ea-98f1-6f57abe8dc86",
            "brand": "Aveo U-VA [2006-2012]"
        },
        {
            "vehicleModelId": "22cd4ada-1b0f-11ea-98f1-5fab5d1a7cd4",
            "brand": "Aveo U-VA [2006-2012]"
        },
        {
            "vehicleModelId": "22cd4adf-1b0f-11ea-98f1-67e74c535c64",
            "brand": "Beat"
        },
        {
            "vehicleModelId": "22cd4ae0-1b0f-11ea-98f1-b99a9c45b99f",
            "brand": "Beat"
        }
    ]

1 个答案:

答案 0 :(得分:1)

这是个窍门,下拉菜单项的值应该唯一以捕获它们而不会出现任何错误

  Padding(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 20.0, vertical: 0.0),
                          child: Container(
                            color: Colors.white,
                            child: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 10.0, vertical: 0.0),
                              child: DropdownButton(
                                  isExpanded: true,
                                  icon: Image.asset('images/drop_down_image.png'),
                                  underline: SizedBox(),
                                  items: vehicleModels.map((dropdownStringItem) {
                                    return DropdownMenuItem(
//value should be unique for each item
                                      value: dropdownStringItem["vehicleModelId"].toString(),
                                      child: Text(dropdownStringItem["brand"]),
                                    );
                                  }).toList(),
                                  onChanged: (String selectedValue) {
                                    setState(() {
                                      loadVariants(vehicelTypeSelected, vehicleBrandSelected, selectedVehicleModel);
                                      this.selectedVehicleModel = selectedValue;
                                    });
                                  },
                                hint: Text("Select Vehicle Model"),
                                value: selectedVehicleModel,
                              ),
                            ),
                          ),
                        ),