'how to hit and get data after getting value from dropdownbutton in flutter

I need to know how to hit API after selecting a value from dropdownButton and how to use that response in text in flutter. Here I can able to hit api and got response but it is using null or value is not changing. I will share image and code.

Dropdown
 Widget partNameDropdown() {
    return SizedBox(
      width: 170,
      height: 50,
      child: DropdownButton<LinishingMaster_Data>(
        borderRadius: BorderRadius.circular(10.0),
        underline: SizedBox(),
        focusColor: AppColors().linenDark,
        elevation: 1,
        enableFeedback: true,
        isExpanded: true,
        style: GoogleFonts.montserrat(
            fontSize: AppSizes().paraLarge16,
            color: AppColors().linenDark,
            fontWeight: FontWeight.w400),
        icon: Icon(
          Icons.keyboard_arrow_down_rounded,
          color: AppColors().linenMedium,
        ),
        items: partName!.map((LinishingMaster_Data item) {
          return DropdownMenuItem<LinishingMaster_Data>(
            child: Text('${item.description.toString()}'),
            value: item,
          );
        }).toList(),
        onChanged: (value) {
          setState(() {
            selectedPartName = value as LinishingMaster_Data;

            entity = getLotNo(selectedPartName!.sId) as LinishingLotNo?;
            seq ? isSelected = true : isSelected = false;
          });
        },
        value: selectedPartName,
        hint: Text("Select item"),
      ),
    );
  }
API
Future<LinishingLotNo> getLotNo(id) async {
    try {
      var res = await repository.linishing_LotNo(id);
      //await getDataUploaded();
      entity = await res;
    } catch (e) {
      print(e);
    }
    seq = true;
    lastLotNo = entity!.last_lot_number;
    print(entity);
    return entity!;
  }
 Widget getAdditionalField() {
    return Column(
      children: [
        SizedBox(
          height: 8,
          width: 80,
          child: TextFormField(
            controller: batchController,
            keyboardType: TextInputType.number,
            validator: (val) {
              if (val!.isEmpty || !RegExp(r'^[0-9]+$').hasMatch(val)) {
                return '';
              } else {
                return null;
              }
            },
          ),
        ),
        SizedBox(
          height: AppSizes().hspacer4 * 1.1,
        ),
        Text(
          lastLotNo ?? "",
          style: GoogleFonts.montserrat(
            fontSize: AppSizes().paraLarge16,
            color: AppColors().linenDark,
          ),
        ),
        SizedBox(
          height: AppSizes().hspacer4,
        ),
        Wrap(
          spacing: 8.0,
          children: List.generate(entity!.lot_suggestion!.length, (index) {
            return InkWell(
              onTap: () {
                setState(() {
                  selectedLotIndex = index;
                  selectedLotValue = entity!.lot_suggestion![index].value;
                  print('----------->LOtVALUE$selectedLotValue');
                });
              },
              child: LotButtons(
                colors: selectedLotIndex == index
                    ? Colors.black54
                    : Colors.grey.shade50,
                borderColor: Colors.black54,
                text: entity!.lot_suggestion![index].display.toString(),
                backgroundColor:
                    selectedLotIndex == index ? Colors.white : Colors.black54,
              ),
            );
          }),
        ),
        SizedBox(
          height: AppSizes().hspacer1,
        ),
        Wrap(
          spacing: 8.0,
          children: List.generate(3, (index) {
            return InkWell(
              onTap: () {
                setState(() {
                  selectedIndex = index;
                  selectedShift = index + 1;

                  print('---------------->>>>>${index + 1}');
                });
              },
              child: ShiftButtons(
                colors: selectedIndex == index
                    ? Colors.black54
                    : Colors.grey.shade50,
                borderColor: Colors.black54,
                text: (index + 1).toString(),
                backgroundColor:
                    selectedIndex == index ? Colors.white : Colors.black54,
              ),
            );
          }),
        ),
      ],
    );
  }
Text to display data which I will get from API call
Widget getSideUiFromBack() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '$empId - $empName',
          style: GoogleFonts.montserrat(
            fontSize: AppSizes().paraLarge16,
            color: AppColors().linenDark,
          ),
          overflow: TextOverflow.clip,
        ),
        SizedBox(
          height: AppSizes().hspacer2,
        ),
        machineDropdown(),
        SizedBox(
          height: AppSizes().hspacer1,
        ),
        partNameDropdown(),// This Dropdown value
        SizedBox(
          height: AppSizes().hspacer2,
        ),
        isSelected ? getAdditionalField() : Container(), // Here it is 
      ],
    );
  }

..................Thank you.........................................



Solution 1:[1]

Hard to help you without some code that we can run, but for sure there are issues on how you assign data in the onChanged part of your dropdown:

    onChanged: (value) async {
      YourDataModel data = await getLotNo(selectedPartName!.sId);
      setState(() {
        selectedPartName = value as LinishingMaster_Data;

        entity =  data as LinishingLotNo?;
        seq ? isSelected = true : isSelected = false;
      });
    },

here you need to await for the result of your getLotNo API call, and then inside your setState assign that result to whatever variable is holding its value

You also need to fix your API call to actually return some data instead of setting it to a variable:

Future<LinishingLotNo> getLotNo(id) async {
    try {
      var res = await repository.linishing_LotNo(id);
      //await getDataUploaded();
    } catch (e) {
      print(e);
    }
    seq = true;
    lastLotNo = res!.last_lot_number;
    print(res);
    return res!;
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AJ989