uploadPrescriptionAPI method

Future uploadPrescriptionAPI({
  1. dynamic context,
  2. dynamic fromPage,
})

Implementation

Future uploadPrescriptionAPI({
  context,
  fromPage,
}) async {
  final apiUrl = fromPage == 'medicine_home'
      ? 'api/medicine/upload-prescription'
      : 'api/medicine/save-selected-prescription';
  try {
    var request = http.MultipartRequest(
      'POST',
      Uri.parse('${WELLNESS_URL}$apiUrl'),
    );

    request.headers.addAll({"Authorization": "$currentUserToken"});

    if (selectedPrescriptionListFromstorage.isNotEmpty) {
      for (int i = 0; i < selectedPrescriptionListFromstorage.length; i++) {
        request.files.add(await http.MultipartFile.fromPath(
          "files[$i]",
          selectedPrescriptionListFromstorage[i].toString(),
        ));
      }
    }

    if (selectedPrescriptionList.isNotEmpty) {
      for (int i = 0; i < selectedPrescriptionList.length; i++) {
        request.fields['prescription[$i]'] = "${selectedPrescriptionList[i]}";
      }
    }

    var response = await request.send();
    var responsed = await http.Response.fromStream(response);

    Get.back();

    if (response.statusCode == 200) {
      var decodedResponse = json.decode(responsed.body);
      if (decodedResponse['success'] == true) {
        selectedPrescriptionListFromstorage.clear();
        getUploadedPrescriptionListFromstorage.clear();

        Validator().successMessage(
          context: context,
          message: "${decodedResponse['message']}",
        );

        if (fromPage == 'medicine_home') {
          Get.off(MedicineHome());
        } else if (fromPage == 'medicine_cart') {
          AlertLoader(context: context);
          await getMedicineAddressList();
          Get.back();
          if (getMedicineAddressData.isEmpty) {
            Get.to(MedicineAddAddressPage(
              addressCategory: 'New',
              addressData: null,
            ));
          } else {
            Get.to(MedicineAddressListPage());
          }
        }
      } else {
        Validator().errorMessage(
          context: context,
          message: "${decodedResponse['message']}",
        );
      }
    } else {
      Validator().successMessage(
        context: context,
        message: "Something Went Wrong. Please try again later !!!",
      );
    }
  } catch (e) {
    print('Exception during file upload: $e');
  }
}