Flutter list files in download directory

60 Views Asked by At

I wrote the following code to list the files in a folder in the downloads folder of the device. As a result, I get an empty list. But the folder is full. Also, while it does not list the files in that folder, it lists the folders in the downloads folder. I couldn't understand where I made a mistake in the code here. Can you help me?

dart

import 'dart:io';

import 'package:external_path/external_path.dart';

Future<String> getPathToDownload() async {
    return ExternalPath.getExternalStoragePublicDirectory(
        ExternalPath.DIRECTORY_DOWNLOADS);
  }

  Future<List<String>> getFilesFromPath() async {
    final downloadDirectory = await getPathToDownload();
    Directory appDocumentsDirectory =
        Directory('$downloadDirectory/test');
    var files = appDocumentsDirectory
        .listSync(recursive: true)
        .whereType<File>()
        .toList();
    log(files.toString());
    List<String> fileNames = [];
    for (var file in files) {
      fileNames.add(
        file.path
            .split("/")
            .asMap()[file.path.split("/").asMap().length - 1]!
            .split(".")
            .first,
      );
    }
    return fileNames;
  }

I tried with the path_provider package, but I could not access the downloads folder on the device with this package. I can access the folders in data. What I want is to directly access the device's downloads folder.

0

There are 0 best solutions below