Flutter Dart File path - flutter

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!

Related

how to display power point file and word file in flutter after getting file path

I listed all docx and pptx files from device storage now I want to open them using file path I tried different packages but I am getting a lot of issues can someone provide me a material related to opening document files or any package that can open files using file path thanks
You can try this package, it will open the files in default application from its path.
https://pub.dev/packages/open_file_plus
https://pub.dev/packages/open_file_plus

How do I access the an assets subdirectory with Directory in 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).

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.

How to embed external exe files in a Flutter project for Windows?

In my Flutter project for Windows, I want to run some .exe files that I want to embed in my project, instead of copying/pasting them by hand in the correct location.
So I tried to add the .exe files in my assets folder, like the following:
assets
\exe
\images
and add the exe folder in my pubspec.yaml file like the following:
flutter:
assets:
- assets/images/
- assets/exe/
The good news is: the exe files are copied in the correct location when I build my project.
The bad news is: I don't know how to get the absolute path of my exe folder in Dart.
So, is there a way to get the absolute path of the exe folder for a Windows project, or is there another way to embed my .exe files so I can get their paths and run them (using Process.start())?
Thanks.
Here is what I did to get the absolute path of the exe folder (only tested on Windows) :
// returns the abolute path of the executable file of your app:
String mainPath = Platform.resolvedExecutable;
// remove from that path the name of the executable file of your app:
mainPath = mainPath.substring(0, mainPath.lastIndexOf("\\"));
// concat the path with '\data\flutter_assets\assets\exe', where 'exe' is the
// directory where are the executable files you want to run from your app:
Directory directoryExe = Directory("$mainPath\\data\\flutter_assets\\assets\\exe")

How to copy file from applicationStorageDirectory into dataDirectory?

I would like to copy an image file located in www/assets/imgs/christmas.jpg to the dataDirectory folder using the Ionic Native File Plugin, however I don't know how to access the www folder. I tried the following:
this.file.checkFile(this.file.applicationStorageDirectory, 'assets/imgs/christmas.jpg')
But it always returns NOT_FOUND_ERR.
You should change applicationStorageDirectory to applicationDirectory (although I am not sure what the difference is) and add www in front of the file path as the application directory contains the www folder and not its contents. So instead of assets/imgs/christmas.jpg use www/assets/imgs/christmas.jpg