【问题标题】:Multiple Dependent dropdown in flutter颤动中的多个依赖下拉列表
【发布时间】:2020-02-20 15:49:50
【问题描述】:

我正在尝试在颤振上构建多个依赖下拉列表。第二个取决于第一个。这是我实现的下拉菜单的代码。

Container(
            child: new DropdownButton<String>(
              underline: SizedBox(
                height: 1,
              ),
              hint: new Text("Select Faculty"),
              value: facultyId,
              items: faculty.map((item) {
                return new DropdownMenuItem(
                  child: new Text(item['name']),
                  value: item['id'].toString(),
                );
              }).toList(),
              onChanged: (faculty == null)
                  ? null
                  : (String newValue) {
                      setState(() {
                        filteredbatch.clear();
                        facultyId = newValue;
                        for (var item in allbatch) {
                          if (item['facultyId'].toString() == newValue){
                          filteredbatch.add(item);
                            disableBatch = false;
                          }
                        }
                      });
                      print(facultyId);
                    },
            ),
          ),
          Container(
            child: new DropdownButton<String>(
                underline: SizedBox(
                  height: 1,
                ),
                disabledHint: new Text("Select Faculty First"),
                hint: Text("Select Batch"),
                value: batchId,
                onChanged: disableBatch
                    ? null
                    : (String newValue) {
                        setState(() {
                          batchId = newValue;
                          disableSection = false;
                        });
                        print(batchId);
                      },
                items: filteredbatch.map((item) {
                  return DropdownMenuItem(
                    child: Text(item['name']),
                    value: item['id'].toString(),
                  );
                }).toList()),
          ),

每当我从第一个下拉项目中选择一个下拉项目时,它会启用第二个下拉菜单并让我从该下拉菜单中选择一个项目。当我从第二个下拉列表中选择一个项目并返回更改第一个下拉列表时,它会引发错误,即下拉列表需要一个具有相应值的项目。找到 0 或 2 个。我在这里迷路了。如何解决此错误?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这里发生的事情非常简单。假设“allbatch”有这些值:

    faculty: foo ,它有 batchids: foo1, foo2, foo3

    faculty: bar ,它有 batchids: bar1, bar2, bar3。

    当您在第一个下拉菜单中选择 foo 时,会创建一个新的“filteredbatch”,它只包含 foo1、foo2、foo3。然后,您在第二个下拉列表中选择 foo3,一切仍然正常...

    但是当您将第一个下拉列表更改为 bar 时,“filteredbatch”仅包含:bar1、bar2、bar3 并且您的第二个下拉列表值仍设置为 foo3,这在新生成的“filteredbatch”中找不到,然后你得到了你所看到的错误。

    要解决此问题,只需在您的第一个下拉 onChanged 方法中更改“filteredbatch”之前将 batchId 设置为 null:

      setState(() {
        //fix
        batchId = null;
        //end of fix
        filteredbatch.clear();
        facultyId = newValue;
        for (var item in allbatch) {
          if (item['facultyId'].toString() == newValue){
          filteredbatch.add(item);
            disableBatch = false;
          }
        }
      });
    

    您的第二个下拉菜单将恢复为提示文本,并且应用用户必须选择一个新的 batchId。

    如果您还有其他问题,请随时提出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2017-11-22
      • 2020-04-27
      • 1970-01-01
      • 2023-04-05
      • 2021-01-19
      相关资源
      最近更新 更多