skip to Main Content

I am working on a project where I want to download a PDF file and display it on screen. I have wrote a below code to achieve it but It gives me error mentioned below. Kindly look at the below error and code and tell me how can I achieve it. I tried many different things but got unsuccessful.

Thanks.

Error:

E/flutter (14805): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DioError [DioErrorType.response]: Http status error [400]
E/flutter (14805): Source stack:
E/flutter (14805): #0      DioMixin.fetch (package:dio/src/dio_mixin.dart:488:35)
E/flutter (14805): #1      DioMixin.request (package:dio/src/dio_mixin.dart:483:12)
E/flutter (14805): #2      DioForNative.download (package:dio/src/entry/dio_for_native.dart:84:24)
E/flutter (14805): #3      OpenBalanceController.getOpenBalancePDF (package:otrack/views/reports/controllers/open_balance_controller.dart:143:26)
E/flutter (14805): <asynchronous suspension>
E/flutter (14805): 
E/flutter (14805): #0      DioMixin.fetch.<anonymous closure> (package:dio/src/dio_mixin.dart:643:7)
E/flutter (14805): #1      _RootZone.runBinary (dart:async/zone.dart:1665:54)
E/flutter (14805): #2      _FutureListener.handleError (dart:async/future_impl.dart:162:22)
E/flutter (14805): #3      Future._propagateToListeners.handleError (dart:async/future_impl.dart:779:47)
E/flutter (14805): #4      Future._propagateToListeners (dart:async/future_impl.dart:800:13)
E/flutter (14805): #5      Future._completeError (dart:async/future_impl.dart:575:5)
E/flutter (14805): #6      _SyncCompleter._completeError (dart:async/future_impl.dart:51:12)
E/flutter (14805): #7      _Completer.completeError (dart:async/future_impl.dart:23:5)
E/flutter (14805): #8      Future.any.onError (dart:async/future.dart:617:45)
E/flutter (14805): #9      _RootZone.runBinary (dart:async/zone.dart:1665:54)
E/flutter (14805): #10     _FutureListener.handleError (dart:async/future_impl.dart:162:22)
E/flutter (14805): #11     Future._propagateToListeners.handleError (dart:async/future_impl.dart:779:47)
E/flutter (14805): #12     Future._propagateToListeners (dart:async/future_impl.dart:800:13)
E/flutter (14805): #13     Future._completeError (dart:async/future_impl.dart:575:5)
E/flutter (14805): #14     Future._asyncCompleteError.<anonymous closure> (dart:async/future_impl.dart:666:7)
E/flutter (14805): #15     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (14805): #16     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

My DIO Api Code:

  Future getOpenBalancePDF() async {
    isLoading.value = true;

    final headers = await BaseClient.generateHeaders();
    Dio dioWithHeaders = Dio(BaseOptions(
        receiveTimeout: 999999, sendTimeout: 999999, headers: headers));
    var dir = await getApplicationDocumentsDirectory();
    String path = "${dir.path}/TransactionSummary.pdf";
    final data = FormData.fromMap({
      "orderType": 0,
      "fromDate": "2000-01-01",
      "toDate": "2023-12-01",
      "customerFilterList": [1387, 1460],
      "exportFormat": 1,
      "openBalance": true
    });
    await dioWithHeaders.download(
      ApiConstants.POST_CUSTOMER_OPEN_BALANCE_REPORT,
      data: data,
      (Headers headers) {
        headers.map.values;
        return path;
      },
    );

    if (path.isNotEmpty) {
      Get.to(
        () => PDFScreen(
          path: path,
        ),
      );
    }
    isLoading.value = false;
  }

2

Answers


  1. You could place the call to await dioWithHeaders.download into a try block, catch the DioError and handle it.

    As a side note: In Dart, Exceptions should normally be caught and handled, whereas Errors should lead to the termination of the program. DioError was deprecated in favour of DioException starting with version 5.2.0, and it seems you are using an old version of Dio.

    try {
      await dioWithHeaders.download(...);
    } on DioError catch (e) {
      // Handle error here: Your error is a network error. 
       
    }
    

    Also, instead of Future (which is in fact Future<dynamic>), the function getOpenBalancePDF should return Future<void>.

    Login or Signup to reply.
  2. Future<void> getOpenBalancePDF() async {
    isLoading.value = true;
    
    final headers = await BaseClient.generateHeaders();
    Dio dioWithHeaders = Dio(BaseOptions(
      receiveTimeout: 999999, sendTimeout: 999999, headers: headers));
    
    try {
     var dir = await getApplicationDocumentsDirectory();
     String path = "${dir.path}/TransactionSummary.pdf";
    
     final data = FormData.fromMap({
      "orderType": 0,
      "fromDate": "2000-01-01",
      "toDate": "2023-12-01",
      "customerFilterList": [1387, 1460],
      "exportFormat": 1,
      "openBalance": true
    });
    
    await dioWithHeaders.download(
      ApiConstants.POST_CUSTOMER_OPEN_BALANCE_REPORT,
      data: data,
      onReceiveProgress: (received, total) {
        // Handle download progress
      },
      options: Options(responseType: ResponseType.bytes), 
      queryParameters: {}, 
      deleteOnError: true, 
      path: path, 
    );
    
      if (path.isNotEmpty) {
        Get.to(() => PDFScreen(path: path));
       }
     } catch (e) {
      print("Error downloading PDF: $e");
     } finally {
      isLoading.value = false;
      }
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search