UploadDignoprescription method

Future UploadDignoprescription({
  1. dynamic context,
})

Implementation

Future UploadDignoprescription({context}) async {
  final apiUrl = 'api/diagnostic/upload-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(
          "prescription[$i]",
          selectedPrescriptionListFromstorage[i].toString(),
        ));
      }
    }

    if (selectedPrescriptionList.isNotEmpty) {
      for (int i = 0; i < selectedPrescriptionList.length; i++) {
        request.fields['prescription_id[$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']}",
        );
        Get.off(DiagnosticsHome());
      } else {
        Validator().errorMessage(
          context: context,
          message: "${decodedResponse['message']}",
        );
      }
    } else {
      Validator().errorMessage(
        context: context,
        message: "Something Went Wrong. Please try again later !!!",
      );
    }
  } catch (e) {
    print('Exception during file upload: $e');
  }
}