'Accesing downloaded files from a Flutter app

I am working on an app that should manage downloaded files. For now I am able to download a file on both platforms, but I would need to know how can I get the downloaded files from the device.

This is the code used to download a file from Internet.

Future<void> downloadFile() async {

    bool downloading = false;
    var progressString = "";

    final imgUrl = "https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
    Dio dio = Dio();

    try {
      var dir = await getApplicationDocumentsDirectory();
      print("path ${dir.path}");
      await dio.download(imgUrl, "${dir.path}/demo.mp4",
          onReceiveProgress: (rec, total) {
            print("Rec: $rec , Total: $total");
            downloading = true;
            progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";


          });
    } catch (e) {
      print(e);
    }
    downloading = false;
    progressString = "Completed";
    
    print("Download completed");
  }

The path print output for Android is:

path /data/user/0/red.faro.labelconciergeflutter/app_flutter

The path print output for iOS is:

path /var/mobile/Containers/Data/Application/9CFDC9E3-D9A9-4594-901E-427D44E48EB9/Documents

What I need is to know how can an app user access the downloaded files when the app is closed or there is no internet connection to open the file from the original URL?



Solution 1:[1]

For getting files from the device you may use file_picker package.

FilePickerResult? result = await FilePicker.platform.pickFiles();

if (result != null) {
  File file = File(result.files.single.path);
} else {
  // User canceled the picker
}

You also can use the File class from the dart:io library.

Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/counter.txt');
}

If your goal is to access commonly used locations on the device’s file system you can use the path_provider plugin.

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William Secchi