2

I have a url that returns a json of companies, i use the list of companies to fill a dropdown. there is another url with a parameter that returns a list of warehouses. The second method that gets the warehouses returns an empty json.

Here is the code that gets the companies. This is working alright

  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";
  }

The code that gets the warehouses is similar.

  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";
  }

And my initState method

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

The dropdowns

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

I am not sure why i am unable to get the values for the second dropdown, when i call the method to get the values for the warehouse in the onchange method in the companies dropdown, the data from the warehouses are printed to the console, but the app crushes.

winfred adrah
  • 428
  • 6
  • 18

1 Answers1

0

I think you miss the async/await, lets try

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,
              ),
Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38