Why does this cause a crash?
final directory = await getApplicationDocumentsDirectory();
final testFile = File("${directory.path}/test.txt");
await testFile.writeAsString("Hello there!", flush: true);
final ByteData bytes = await rootBundle.load(testFile.path);
The rootBundle.load() call causes:
Unhandled Exception: Unable to load asset: /data/user/0/com.flutter.tests/app_flutter/test.txt
BUT if I do a hot reload then it works fine until I restart the app.
I have a dependency path_provider: any in pubspec.yaml which is needed for the getApplicationDocumentsDirectory().
rootBundle only contains assets that were specified by pubspec.yaml and were built with your application. It's not supposed to access files created within file system. Use File or Image classes to open created files.
final bytes = testFile.readAsBytesSync();
The rootBundle contains the resources that were packaged with the
application when it was built. To add resources to the rootBundle for
your application, add them to the assets subsection of the flutter
section of your application's pubspec.yaml manifest.
Related
I am making a flutter app in which I have to call some APIs.
I have added .env file but its still giving exception
FlutterConfig Variables are empty.
Ensure you have a .env file and you have loaded the variables.
How to solve this ?
Use the library flutter_dotenv
Add the .env file to your assets bundle in pubspec.yaml.
assets:
- .env
after in main.dart
void main() async {
...
await dotenv.load();
...
}
in app
dotenv.get('VAR_NAME')
Let's say I have a file named some_file.json defined in pubspec.yaml as an asset. In my flutter project I load the file with the loadString function.
await rootBundle.loadString("some_file.json");
Then I read values from the file and I need to create Unit Tests. In the Unit Test project I create file with MemoryFileSystem class.
final MemoryFileSystem memoryFileSystem = MemoryFileSystem();
final File file = memoryFileSystem.file("some_file.json");
file.createSync(recursive: true);
expect(file.existsSync(), true);
The code above successfully creates file, but load method is not able to open the file.
Error:
237:7 PlatformAssetBundle.load
Unable to load asset: build_resources/variables.json
Is there any better approach how to mock asset file in Unit Tests project?
I have created a dotEnv file in root of project and added that file to pubspec.yaml file like this:
assets:
- dotEnv.develop
then I reference it in main.dart file this way:
void main() async{
await dotenv.load(fileName: "dotEnv.develop");
runApp(const MyApp());
}
but the the released app can not find dotEnv.develop file, however it exists in assets folder as this image shows:
and in console of chrome I get this error:
main.dart.js:41783 Error while trying to load an asset: Failed to load asset at "assets/dotEnv.develop" (404)
Failed to load resource: the server responded with a status of 404 (Not Found)
now my flutter sdk version is 3.0.5
The problem is that the dotEnv.develop is in the root folder and you're trying to load it from the assets folder.
Instead of:
await dotenv.load(fileName: "assets/dotEnv.develop");
Do the following instead:
await dotenv.load(fileName: "dotEnv.develop");
Create a .env file inside of the root of your project
and write this at it
API_URL=localhost:3000
then add the .env file to pubspec.yaml
assets:
- .env
this is resource
At the moment i build a chat similar in wahtsapp.
I would like upload some local files (pdf, txt, image...).
Like this:
I try to get the local files via path_provider.
But i dont get access to download folder.
My problem:
I don't know how i can access the dowload folder with path_provider.
See my code:
//load local files from download folder
_loadLocalFile() async {
//absoulute path to download file
var file = io.Directory('/storage/emulated/0/Download/')
.listSync(); //use your folder name insted of resume.
//console output -> []
//other try to get download folder
List<io.Directory>? documents = await getExternalStorageDirectories();
//console output -> [Directory: '/storage/emulated/0/Android/data/de.securebuy.securebuy/files', Directory: '/storage/1BEC-0908/Android/data/de.securebuy.securebuy/files']
print(documents.toString());
}
I also add to AndroidManifest following rules:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGEā/>
Anyone have a idea how i can build a function similar in image?
Is that possible?
Many thx
till now , pathprovider isn't going to let you in downloads file , so if you are on android you can just write the downloads directory , else using android_path_provider it is a null-safety package and you can accsess it via :var downloadsPath = await AndroidPathProvider.downloadsPath; , on ios you can get only the app directory cuase ios makes it's own file for every app thus we use 'pathprovider' with Directory appDocDir = await getApplicationDocumentsDirectory();
Update :
i see what you are trying to achieve and this is not what pathprovider pcakage is used for , you have to use file_picker , read thier docs , i thinks that this is what you need
I am creating a Flutter Desktop application for windows and I am trying to retrieve the application directory where the exe is located. I tried to use path_provider but can only get the Documents directory. Any help I would appreciate lots.
Use Platform.resolvedExecutable
(of dart:io).
I have tested it in Flutter/Windows and it works.
Strangely, Platform.executable returns null instead. I tell strangely because its type is String not nullable. This unexpected null value can cause crash or errors that are difficult to detect (but in the end Flutter desktop is not in the stable channel).
String dir = Directory.current.path;
print(dir);
Directory.current.path (in the dart:io package) returns the current working directory of the project folder (when developing) or the executable directory when running from a release build.
Try is package: path_provider
set this code in pubspec.yaml
dependencies:
path_provider: ^1.6.27 # get last Version From https://pub.dev/packages/path_provider/install
Code
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path; //C:\Users\USER_NAME\AppData\Local\Temp
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path; //C:\Users\USER_NAME\Documents
//Warning: on debug mode this code get location project but in release mode will get your location app correctly
String dir = Directory.current.path; // PATH_APP