I got file response from API
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.
2
Answers
I managed to open it in this way.
First, you need to correct the retrofit response api to
HttpResponse<List<int>>
like this:And here is the rewrite code to save the file: