skip to Main Content

I got file response from API

enter image description here

I want to generate it as PDF and open using open_filex

This is what I have tried

@override
  Future<Either<Failure, dynamic>> getInvoicePDF(GetInvoiceParams param) async {
    return await Factory().exceptionsGuard(() async {
      var res = await RemoteDataSourceResponseGuard.execute<dynamic>(
        _invoiceRemoteDataSource.downloadInvoicePDF(
            sessionToken: param.sessionToken,
            userId: param.userId,
            invoiceId: param.invoiceId),
      );

      await generatePDF(res);
    });
  }

  Future generatePDF(dynamic value) async {
    debugPrint("file response $value".toString());
    String dir = (await getApplicationDocumentsDirectory()).path;

    File file = File('$dir/"file".pdf');

    await file.writeAsBytes(value);

    try {
      OpenFilex.open(file.path);
    } catch (e) {
      debugPrint("error $e");
    }
  }

InvoiceRemoteDataSource

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

part 'invoice_remote_data_source.g.dart';

@RestApi()
abstract class InvoiceRemoteDataSource {
  factory InvoiceRemoteDataSource(
    Dio dio, {
    String baseUrl,
  }) = _InvoiceRemoteDataSource;

  @POST('/download_invoice_pdf')
  Future<dynamic> downloadInvoicePDF({
    @Field("session_token") required String sessionToken,
    @Field("user_id") required String userId,
    @Field("invoice_id") required String invoiceId,
  });
}

The value display in log is %PDF-1.4

Here the error

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'List<int>'

This is the part of the response which shows in log.

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I managed to open it in this way.

    Future<File> generatePDF(GetInvoiceParams param) async {
        var request = await HttpClient().getUrl(Uri.parse(
            "url"));
        var response = await request.close();
        var bytes = await consolidateHttpClientResponseBytes(response);
        String dir = (await getApplicationDocumentsDirectory()).path;
        File file = File('$dir/pdfName.pdf');
        await file.writeAsBytes(bytes);
        return file;
      }
    
     // then open the file path using openx 
    

  2. First, you need to correct the retrofit response api to HttpResponse<List<int>> like this:

     @DioResponseType(ResponseType.stream)
     @POST('/download_invoice_pdf')
       Future<HttpResponse<List<int>>> downloadInvoicePDF({
       @Field("session_token") required String sessionToken,
      @Field("user_id") required String userId,
      @Field("invoice_id") required String invoiceId,
    });
    

    And here is the rewrite code to save the file:

      var res = await  RemoteDataSourceResponseGuard.execute<HttpResponse<List<int>>>(
        _invoiceRemoteDataSource.downloadInvoicePDF(
            sessionToken: param.sessionToken,
            userId: param.userId,
            invoiceId: param.invoiceId),
      );
      
      final bytes = res.data;
      await generatePDF(bytes);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search