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

问题描述 投票:0回答:1

有两个 Dropdownbuttonformfield,当从第一个 dropdownbuttonformfield 选择服务时,它会调用提供商,并调用 API 来获取根据服务 id 提供该服务的医生列表。API 正在被调用并正确获取响应,但是当我点击第二个 dropdwnfrmfild 时用于医生列表,它不会更新医生列表并显示以前的医生列表

// 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;
    }),
  ],
),
flutter dart widget provider state-management
1个回答
0
投票

检查 flutter Provider 示例。 你应该notifyListeners(); “刷新”监听提供者的小部件。

© www.soinside.com 2019 - 2024. All rights reserved.