List local files in directory with Flutter

1,190

You're mixing up two different things:

  1. flutter assets come with the app and cannot be changed by the app (to my knowledge not browsed either).
  2. getApplicationDocumentsDirectory() returns the documents directory of your app. Here your app can store files it gets during runtime.

If you have a number of wav files in your assets, you can load them as follows:

// to get the asset as bytes
String text = await rootBundle.loadString('/assets/text/randomText.txt');
// to get the asset as string
ByteData bytes = await rootBundle.load('/assets/music/randomAudio.wav');

To get a list of all assets, I think it is necessary to create an array with all paths:

List<String> audioAssets = [
    "assets/audio/categoryDir/something.wav"
    "assets/audio/categoryDir/other.wav"
];

Update: There is actually a way to display the list of all included assets.

final String manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
List<String> images = json.decode(manifestJson).keys.where((String key) {
    return key.startsWith('/assets/audio/categoryDir');
}).toList();

Found the answer here: Flutter read all files from asset folder.

I hope this is finally what you were looking for.

Share:
1,190
Plotisateur
Author by

Plotisateur

BY DAY : I LIVE BY NIGHT : I LIVE Welcome

Updated on December 21, 2022

Comments

  • Plotisateur
    Plotisateur over 1 year

    I'm beginning to have a headache understand the underlying Flutter FileSystem...

    I'm trying to simply list the content of a directory (having audio files) and used every lib I found to get the right path without success... This is my structure :

    root
      D assets
        D audio
          D categoryDir
            F something.wav
            F other.wav
    

    This is the assets part of my pubspec.yaml :

     assets:
      - assets/audio/
      - assets/audio/categoryDir/
    

    I already used getApplicationDocumentsDirectory and Directory.list but I always have file not found error by using this :

    Directory directory = await getApplicationDocumentsDirectory();
    Directory audioDir = new Directory(directory.path + '/assets/audio/categoryDir')
    audioDir.list(recursive: true, followLinks: false)
      .listen((FileSystemEntity entity) {
        print('Files: ' + entity.path);
    });
    

    How to do ?

  • Plotisateur
    Plotisateur almost 4 years
    That's exactly my problem, I know I can load easily each path with rootBundle.load but what if I don't know the file I want to read ? and just want to list all files in a certain directory
  • josxha
    josxha almost 4 years
    You add the files manually to your assets folder while programming the app. Once the app has been compiled, no changes can be made here. Therefore you know the names of the files. If you let the user decide which file to load when the app is compiled, you can show him a list of the names as shown in my last code snippet. But you have to insert this list yourself before. Does that answer your question?
  • Plotisateur
    Plotisateur almost 4 years
    Thanx for your answer but not really..My case is only to READ, not write on the file so there should be no problem even if it's at runtime. I need to know the name of each files (because I use them in my algo) in the directory, and I don't know them in advance !
  • josxha
    josxha almost 4 years
    I read in a little and actually found an answer to your question. I edited my answer.
  • Plotisateur
    Plotisateur almost 4 years
    Thanx you that's exactly the thing ! ;)