How do I access the an assets subdirectory with Directory in Flutter - flutter

I am working on an app in Flutter where I will have to find filenames that start with specific keywords in a directory (and then do things with those filenames subsequently). However, I am encountering trouble accessing the directory itself.
I have an 'assets' folder in the root directory of my project, as many online sources recommended, and a subdirectory in that directory. I include this subdirectory in my pubspec.yaml in the format 'assets/[subdirectory name]', and can generally access its contents with 'assets/[subdirectory name]/[filename]' with methods like loadString. However, when I try to access the directory like this: Directory('assets/subdirectory/'), a FileSystemException no such directory error is thrown. I have tried the usual combination of adding './' or removing the final slash, to no avail.
Are Directory filepaths relative to something else? Is there a better way to do this? Any help would be appreciated.

Directory() operates on the filesystem. You can't access assets with it because assets are bundled with the app during the build process.
During a build, Flutter places assets into a special archive called the asset bundle that apps read from at runtime.
https://docs.flutter.dev/development/ui/assets-and-images#asset-bundling
To get a list of assets in the app, you can use the undocumented generated file AssetManifest.json which contains a map of all bundled assets:
final assetsManifest = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final assets = json.decode(assetsManifest).keys;
Another option (perhaps cleaner, since AssetsManifest.json is AFAIK indeed undocumented and might change in future versions) would be to simply make your own list of assets and save it as another asset (JSON file).

Related

C library can't find it's .cfg file on iOS

I have compiled the labstreaminglayer library for an iOS app, which works fine. But I would like to do some configuration as described here. But I can't get the library to find the lsl_api.cfg file.
Just putting it in any of the folders in XCode doesn't work, even though I can see that is there by searching for it and printing the path. I can set the LSLAPICFG environment variable but again anything I can think to put in there for a relative path doesn't point to the right place and I can't find any other env variable in this list which gives the absolute path...
With the benefit of a nights sleep I realised I can just edit liblsl and add some logging to figure out what is going on. So liblsl tries to look for the config file in the ~ folder, giving the path:
/private/var/mobile/Containers/Data/Application/GUID1/
While the lsl_api.cfg file is copied to:
/private/var/containers/Bundle/Application/GUID2/APPNAME.app/lsl_api.cfg
And libsl can't seem to access files in the GUID2 folder at all, something to do with sandboxing perhaps?
But both liblsl and the App can both access the /Documents folder (perhaps because I enabled iTunes File Sharing?) so I can just chuck the cfg file in there and update libsl to find it. With the added benefit that I can just edit it on my mac.
So I'm still not sure what I would do if I didn't want to put the cfg file in the /Documents folder but at least this works!

How can I save a local file into assets folder in Flutter

My flutter desktop application takes a local file (audio/video/image) as user input. Now I need to save it to somewhere for later usage. So that even if the device changes the application can still access them using relative path.
What I want to do is copy this file to assets folder using file.copy method. But how can I get the assets folder's directory? I don't want to hardcode assets folder's absolute path. Is there any way to copy files into assets folder using relative path?
Or any alternate way to do the task?
assets is read-only. Use documents directory where the application can store files that only it can access.
final directory = await getApplicationDocumentsDirectory();
See Read and write files for details.

Flutter Dart File path

quick Flutter Dart issue.
I am trying to read from a text file located inside the app's directory, however Flutter does not recognize a single file path that I provide it. The file path is always responded to with "OS Error: No such file or directory, errno = 2".
I am trying to read from a file located in assets/importfiles, from a dart class inside lib/viewmodels. The assets directory has already been added to pubsec.yaml.
Here is the picture of my directory structure
I can even have the main class locate and print out the files' paths using rootBundle, but trying to initialize a new File using those paths provides the same error text.
If I were to use path provider to locate and read from files, how would I initially put the files into the Temporary Directory or Application Documents Directory, if Flutter does not recognize the file paths of the files that I want to insert?
Just You need to remove folder name ''.
assets\importfiles
to
assets Or importfiles
Thanks You.Happy To help you
Have you tried putting a "/" before "assets" in the file path?
I'm not sure that this will fix the error, let me know if it works!

Can Google Package App use external directories during packing?

I am writing a number of Google Packaged Apps which run independently, but share lots of code. For example, they all use "library.js". I would like to have only one copy of library.js so any changes to it will be used by all newly packed apps.
To package my apps, it seems they all must have a copy of library.js in their own directory structure, whereas it would be nice to have a single master copy in some other directory that is accessible to all. I currently do a manual check to make sure all files are up-to-date before packing, and I am writing some code to do the check automatically, but it seems like a work-around.
Can a Google Packaged App use JS code in external library directories, or must all code be under the root directory of the app (i.e., requiring copying from external directory) when packing?
Have you tried providing a URL i.e. host the javscript file in .js format to an accessible location to your apps and then provide the .js file URL in all your apps code. The very next time you want to change, all you have to do is to update that .js file.

Exclude files or folders while cfx xpi - Firefox addon sdk

Is there any way to exclude a folder while creating an xpi file using cfx xpi. The documentation doesnt provide any info on this.
I am asking this because in the directory created by cfx init i also have a directory as HTMLTestPages in which i have some test JavaScript since we cannot debug the add-on sdk JavaScript.
Every time i have to create an xpi file i have to remove the folder and again place it. furthermore, if we has this folder on svn this is more problematic.
searching on google shows this bug listed to exclude vim temporary files and in the comments they are also discussing excluding any files starting with .(period). So is there any way to check if this is working in add-on sdk 1.6.1?
The current Add-on SDK version will only add very specific subdirectories to the XPI package, namely data and lib. If your project also has a subdirectory foo or files at the top level with names that the SDK doesn't recognize - these will be simply ignored.
Now if your files are located inside a known subdirectory like data - in this case they will be included in the package. The Add-on SDK has hardcorded rules concerning files and directories that should be excluded, you can see them under python-lib/cuddlefish/util.py:
IGNORED_FILE_PREFIXES = ["."]
IGNORED_FILE_SUFFIXES = ["~", ".swp"]
IGNORED_DIRS = [".git", ".svn", ".hg"]
This means that file names starting with a period or ending with either ~ or .swp will be ignored. But as far as directories goes - only .git, .svn and .hg will be ignored (and this definitely works). If you want an additional directory to be ignored then the only way will be to edit python-lib/cuddlefish/util.py and add it to IGNORED_DIRS list (something that you will unfortunately have to repeat after each SDK update).