getSearchHospitalList method

Future getSearchHospitalList({
  1. dynamic hospitalName,
  2. dynamic pincode,
  3. dynamic city,
  4. dynamic stateName,
  5. dynamic policyId,
  6. required dynamic context,
  7. dynamic currentPage,
  8. bool ispagination = false,
})

Implementation

Future getSearchHospitalList({
  hospitalName,
  pincode,
  city,
  stateName,
  policyId,
  required context,
  currentPage,
  bool ispagination = false,
}) async {
  try {
    final response = await apiRequest(
      'GET',
      '${APP_URL}hospital-list?policy_id=$policyId&state=${stateName ?? ''}&city=${city ?? ''}&hospital_name=${hospitalName ?? ''}&page=${currentPage ?? '1'}&pincode=${pincode ?? ''}',
      null,
    );

    Get.back(); // Close the loader

    if (response['success'] == true) {
      WholeHospitalData = response['data'];
      if (ispagination) {
        // Append new data to existing list for infinite scroll
        HospitalList = [...HospitalList, ...response['data']['data']];
      } else {
        // Replace list for initial load or new search
        HospitalList = response['data']['data'];
        searchHospitalList = response['data']['data']; // Reset search list
        Get.to(
          SearchHospitalList(
            pincode: pincode ?? '',
            hospitalName: hospitalName ?? '',
            city: city ?? '',
            stateName: stateName ?? '',
          ),
        );
      }
      return response['data']; // Return data for potential further processing
    } else {
      Validator().errorMessage(
        context: context,
        message: response['message'] ?? 'Failed to load hospital list',
      );
      return null;
    }
  } catch (e) {
    Get.back(); // Ensure loader is closed on error
    Validator().errorMessage(
      context: context,
      message: 'An error occurred while fetching hospital list',
    );
    return null;
  }
}