Reputation: 379
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()
.
Upvotes: 2
Views: 832
Reputation: 9873
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.
Upvotes: 2