在从flutter中获得值的价值后,如何击中和获取数据

发布于 2025-01-26 20:10:09 字数 5161 浏览 1 评论 0原文

我需要知道如何从dropdownbutton选择一个值以及如何在颤音中使用该响应后击中API。在这里,我能够击中API并得到响应,但是它使用了空或值没有变化。我将共享图像和代码。

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 
      ],
    );
  }

..................谢谢.............................. .............

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]+

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

).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.........................................

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2025-02-02 20:10:09

在没有我们可以运行的一些代码的情况下,很难为您提供帮助,但是可以肯定的是,您如何在on Changed下拉下拉的一部分中分配数据存在问题:

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

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

在这里您需要等待为了您的getLotno api调用,然后在setState内部分配结果,将结果分配给保存其值的任何值,

您还需要修复API调用以实际返回一些数据而不是将其设置为变量:

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

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!;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文