How to download pdf to mobile device flutter


Hello I have this method that gets pdf from api and returns decoded file, I am able to display it on click, but i want to save it to device.
Here is my method for decoding pdf:

  Future<File> _storeFile(String bytes) async {
    final filename = "${_dokument?.naziv}";
    final bytes = _dokument?.bytes;
    Uint8List base64Decode(String source) => base64.decode(source);

    await checkDocumentFolder();

    String dir = directory!.path;

    //final file = File('${dir.path}/$filename');
    final file = File('$dir/$filename');
    //var pdfBytes = bytes?.split(",")[1];
    if (!file.existsSync()) file.create();
    var pdfBytes = bytes?.substring(bytes.indexOf(",") + 1);
    await file.writeAsBytes(base64Decode(pdfBytes!), flush: true);

    return file;

So my question is, can i somehow save file returned from this method to mobile device.

>Solution :

yes you can store your file in device
i suggest you to see this article and this

also you can use file_saver package to store file all platform flutter support

Leave a ReplyCancel reply