dart Flutter提供程序未更新小部件数据并显示以前的值

b09cbbtk  于 2024-01-03  发布在  Flutter
关注(0)|答案(1)|浏览(171)

有两个Dropdownbuttonformfield,当从第一个dropdownbuttonformfield中选择服务时,它调用提供者,并调用API以获取基于服务ID提供该服务的医生列表。API被调用并正确获得响应,但当我点击第二个dropdownfrmfild(用于医生列表)时,它不会更新医生列表并显示以前的医生列表

// Define the function to get the list of doctors from the entity
void getListDoc(ListDoctorEntity? entityDoctor) {
  for (int i = 0; i <= entityDoctor!.result.length - 1; i++) {
    doctorList1.add('${entityDoctor.result[i].firstname} ${entityDoctor.result[i].lastname}');
  }
  setState(() {
    doctorList = doctorList1;
  });
  print('doctor list new ++++++++++++++++ --------------$doctorList');
}

// Define the function to get the doctor based on service and update UI
void getDoc(int id, String service, BuildContext context, ListDoctorProvider provider) {
  doctorList = ['Select doctor'];
  doctorIs = 'Select doctor';
  doctorList1 = ['Select doctor'];
  ListDoctorEntity? doctorEntity;
  provider.eitherFailureOrListDoctor(serviceId: id, clientid: clientID);
  doctorEntity = Provider.of<ListDoctorProvider>(context, listen: false).listdoctor;
  provider.eitherFailureOrListDoctor(serviceId: id, clientid: clientID);
  doctorEntity = Provider.of<ListDoctorProvider>(context, listen: false).listdoctor;
  getListDoc(doctorEntity);
}

// Define the structure of the DropdownButtonFormField
Container buildDropdownContainer(String labelText, List<String> items, String value, Function onChangedCallback) {
  return Container(
    height: MediaQuery.of(context).size.height * 0.08,
    width: MediaQuery.of(context).size.width * 0.9,
    decoration: const BoxDecoration(color: Colors.white),
    child: DropdownButtonFormField(
      style: Theme.of(context).textTheme.bodyMedium,
      decoration: InputDecoration(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16.0),
        labelText: labelText,
        labelStyle: Theme.of(context).textTheme.bodySmall,
        hintText: labelText,
        hintStyle: Theme.of(context).textTheme.bodySmall,
        errorStyle: const TextStyle(fontSize: 0.05),
        border: const OutlineInputBorder(
          borderRadius: BorderRadius.all(Radius.circular(4)),
        ),
      ),
      items: items.map((item) => DropdownMenuItem<String>(
        value: item,
        child: Text(
          item,
          style: Theme.of(context).textTheme.bodyMedium,
        ),
      )).toList(),
      value: value,
      onChanged: (value) {
        onChangedCallback(value);
        setState(() {
          labelText == 'Service' ? thisservice = value : doctorIs = value;
        });
      },
    ),
  );
}

// Usage of the DropdownButtonFormField
Column(
  children: [
    buildDropdownContainer('Service', servicelist12, thisservice, (value) async {
      var listdoctorprovider = Provider.of<ListDoctorProvider>(context, listen: false);
      for (int i = 0; i <= sList.length - 1; i++) {
        if (sList[i].name == value.toString()) {
          sid = sList[i].id;
          charges.text = sList[i].amount.toString();
          print('service id is===================$sid');
        }
      }
      await getDoc(sid, value.toString(), context, listdoctorprovider);
      serviceChanged = true;
      print('doctor after service --------------$doctorIs');
    }),

    // Conditionally display a SizedBox based on screen orientation
    screenOrientation.toString() == 'Orientation.landscape'
        ? sizedbox
        : const SizedBox(),

    buildDropdownContainer('Select doctor', doctorList, doctorIs, (value) {
      doctorval = value.toString();
      serviceChanged = false;
    }),
  ],
),

字符串

piv4azn7

piv4azn71#

检查flutter Provider exemple。您应该notifyListeners();来“刷新”侦听提供程序的小部件。

相关问题