【问题标题】:Get dropdown values based on another dropdown value in flutter根据flutter中的另一个下拉值获取下拉值
【发布时间】:2022-01-22 20:35:29
【问题描述】:

我有一个返回公司 json 的 url,我使用公司列表来填写下拉列表。还有另一个带有参数的 url,它返回仓库列表。获取仓库的第二种方法返回一个空的 json。

这是获取公司的代码。这个没问题

  String? _mySelection;
  final String url = "http://url:8000/companies"
  Future<String> getCompanies() async {
    var res = await http
        .get(Uri.parse(url), headers: {
      'Content-Type': 'application/json'
    });
    var resBody = json.decode(res.body)["data"];    // data = map["data"];
    print(resBody);
    setState(() {
      data = resBody;
    });
    print(res);
    return "Sucess";
  }

获取仓库的代码类似

  Future<String> getWarehouses(company) async {
    late String warehousesUrl = "http://myWarehouseurl?company=$company";
    var warehouseRes = await http
        .get(Uri.parse(warehousesUrl), headers: {
      'Content-Type': 'application/json'
    });
    var warehouseResBody = json.decode(warehouseRes.body)["data"];    // data = map["data"];
    print(warehouseResBody);
    setState(() {
      warehouseData = warehouseResBody;
    });
    print(warehouseRes);
    return "Sucess";
  }

还有我的 initState 方法

  @override
  void initState() {
    super.initState();
    this.getCompanies();
    this.getWarehouses(_mySelection);
  }

下拉菜单

              new DropdownButton(
                items: data.map((item) {
                  return new DropdownMenuItem(
                    child: new Text(item['company_name']),
                    value: item['company_name'].toString(),
                  );
                }).toList(),
                onChanged: (newVal) {
                  setState(() {
                    _mySelection = newVal.toString();
                    getWarehouses(_mySelection);

                  });
                },
                value: _mySelection,
              ),
              _mySelection != "" ? DropdownButton(
                items: warehouseData.map((item) {
                  return new DropdownMenuItem(
                    child: new Text(item['warehouse_name']),
                    value: item['name'].toString(),
                  );
                }).toList(),
                onChanged: (newVal) {
                  setState(() {
                    _mySelection = newVal.toString();
                  });
                },
                value: _mySelection,
              ) : Container(),

我不确定为什么我无法获取第二个下拉列表的值,当我在公司下拉列表的 onchange 方法中调用该方法以获取仓库的值时,来自仓库的数据被打印到控制台,但应用崩溃了。

【问题讨论】:

标签: flutter dropdown


【解决方案1】:

我想你错过了async/await,让我们试试

new DropdownButton(
                items: data.map((item) {
                  return new DropdownMenuItem(
                    child: new Text(item['company_name']),
                    value: item['company_name'].toString(),
                  );
                }).toList(),
                onChanged: (newVal) async{ // here need the change
                  setState(() {
                    _mySelection = newVal.toString();
                    await getWarehouses(_mySelection); // also here add the await

                  });
                },
                value: _mySelection,
              ),

【讨论】:

  • 在方法调用 getWarehouses() 之前添加等待意味着我必须将异步添加到 setState 而不是 onChange 方法
  • 你可以这样做,但问题是你得到解决方案了吗?
猜你喜欢
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多