0

so I'm trying to work with download manager in flutter using flutter_downloader package. I manage to make my app download the file I wanted. How can I make my app more advance ? I wanted my app to be able to open the downloaded file automatically when the download is done.

Another question is, if I already download the file first time how can I make the button trigger to open the file location rather than downloading the file again ?

Here's my current code:

  TextButton(
    child: Text(
      ticketData['file_attachment_map'][index]['name'],
      style: primaryColor600Style.copyWith(fontSize:14.sp),
    ),
    
    onPressed: () async {
      final permissionStatus = await Permission.storage.request();
    
      if (permissionStatus.isGranted) {
      final externalDir = await getExternalStorageDirectory();
    
      final taskId =await FlutterDownloader.enqueue(
        url: ticketData['file_attachment_map']
        [index]['url'],
        savedDir:externalDir!.path,
        showNotification:true,
        openFileFromNotification:true,
        );
      } else {
        print('Permission denied');
      }
    },
  ),
Kim San
  • 575
  • 7
  • 22

2 Answers2

0

Use open_file package after downloaded

if(taskId != null){
    await OpenFile.open(filePath);
  }
}

And you can check file exist before download file

import 'dart:io';
File("path/to/file").exists() 
Xuuan Thuc
  • 2,340
  • 1
  • 5
  • 22
0

As a complement to @Xuann Thucc answer, you can use the open_filex which is a fork of open_file but is better maintained and fixes many issues with the original lib.

import 'package:open_filex/open_filex.dart';

// in async function
await OpenFilex.open(filePath);
DurandA
  • 1,095
  • 1
  • 17
  • 35