flutter_downloader icon indicating copy to clipboard operation
flutter_downloader copied to clipboard

iOS 14.5 download fails, error message: only success task can be opened

Open javitrujillo99 opened this issue 3 years ago • 2 comments

This is my code:

PDFDocument document; ReceivePort _receivePort = ReceivePort(); double progress = 0; static const String _named = 'downloading';

static void downloadCallback( String id, DownloadTaskStatus status, int progress) { SendPort port = IsolateNameServer.lookupPortByName('$_named'); port.send(progress); }

@override void initState() { super.initState(); loading = true; IsolateNameServer.registerPortWithName(_receivePort.sendPort, '$_named');

_receivePort.listen((message) {
  print(message);
  setState(() {
    progress = message + .0;
  });
});

FlutterDownloader.registerCallback(downloadCallback);
this.getPdf();

}

@override void dispose() { super.dispose(); IsolateNameServer.removePortNameMapping('$_named'); this._receivePort.close(); FlutterDownloader.cancelAll(); }

void getPdf() { PDFDocument.fromURL( '${this.widget.url}', ).then((value) { setState(() { this.document = value; loading = false; }); }).catchError((error) { setState(() { this.loading = false; this.error = error.toString(); }); }); }

And the button: GestureDetector( onTap: () async { _loading(); ApiClient apiClient = getIt.getItInstance<ApiClient>(); await apiClient.downloadFile(this.widget.url); Navigator.pop(context); },

This is the downloadFile function: dynamic downloadFile(String path) async { if (Platform.isAndroid) { var status = await Permission.storage.status;

  if (status != PermissionStatus.granted) {
    await Permission.storage.request();
  } else {
    print('No permission');
  }
}

Directory appDocDir = Platform.isAndroid
    ? await getExternalStorageDirectory()
    : await getApplicationDocumentsDirectory();

String pathToSave = Platform.isAndroid
    ? '${appDocDir.path.split('/Android/')[0]}/Download/'
    : '${appDocDir.path}';

List pathSplit = path.split('.');

final taskId = await FlutterDownloader.enqueue(
  url: '$path',
  savedDir: '$pathToSave',
  fileName: '${Uuid().v4()}.pdf',
  showNotification: true,
  openFileFromNotification: true,
);

await FlutterDownloader.open(taskId: taskId);

return taskId;

}

javitrujillo99 avatar Jan 20 '22 12:01 javitrujillo99

Hi there, Any update on this ? I am also getting the same error

flutter_downloader: ^1.7.1 Flutter version 2.10.4

arunraj-qburst avatar Apr 10 '22 18:04 arunraj-qburst

You can't open file from a download task which is not successful.

bartekpacia avatar May 23 '22 15:05 bartekpacia