getBillAsPDF method

Future<AnybillResult<List<int>>> getBillAsPDF(
  1. {int requestCount = 2,
  2. required String billId}
)

Generates a PDF file for a given bill

  • requestCount can be used to determine the number of repeated requests before a failure is returned if the call is not successful.

  • billId of the bill that is supposed to be exported in a PDF format.

Returns a AnybillResult.success that contains the status code and a list of bytes that enables to either export it using the native share view or save it directly to the device via the file picker view.

Throws a AnybillResult.failure with the specified information gained from the API. This could be a DioException when a generic errors occurred or a different Exception for critical errors that couldn't be caught.

Implementation

Future<AnybillResult<List<int>>> getBillAsPDF({
  int requestCount = 2,
  required String billId,
}) async {
  AnybillLogger.info("Tried to export bill $billId");
  try {
    final token = await _tokenProvider.getToken();

    if (token == null) {
      return AnybillResult.failure(
        type: AnybillErrorType.noUserError,
      );
    }

    // Makes a request to export the given bill
    final pdfBytes = await _billService.getBillPdfWithBillId(
      token: token,
      billId: billId,
    );

    return AnybillResult.success(
      HttpStatus.ok,
      data: pdfBytes,
    );
  }
  // ----------------------------------------------------------------
  catch (error, stacktrace) {
    return ErrorHandler.handleError(
      error,
      stacktrace,
      requestCount: requestCount,
      request: () => getBillAsPDF(
        requestCount: requestCount - 1,
        billId: billId,
      ),
      event: "getBillAsPDF",
      library: runtimeType.toString(),
    );
  }
}