this is my project structure:
your_project/
|-- android/
|-- ios/
|-- lib/
|-- assets/
|-- test0.json // **Can access**
| |-- file1.png
| |-- file2.json
| |-- ...
| |-- jsons/
| |-- test.json // **Can't access**
| |-- file3.json
| |-- file4.json
| |-- ...
|-- pubspec.yaml
|-- ...
this is my pubspec.yaml:
flutter:
uses-material-design: true
assets:
- assets/
- assets/images/
- assets/jsons/
Reading the file:
Future<void> initSupportFiles() async {
try {
// Get the Application Support directory path
Directory appSupportDir = await getApplicationSupportDirectory();
String appSupportPath = appSupportDir.path;
// Check if the JSON file already exists in the Application Support directory
String jsonFilePath = '$appSupportPath/jsons/test.json';
File jsonFile = File(jsonFilePath);
if (await jsonFile.exists()) {
return;
}
// Read the JSON file from assets directly
ByteData data = await rootBundle.load('assets/jsons/test.json');
final buffer = data.buffer;
await jsonFile.writeAsBytes(buffer.asUint8List());
print('JSON file copied to Application Support directory.');
} catch (e) {
print('Error copying JSON file: $e');
}
}
My problem: I can’t access ANY files inside subfolders inside Assets (only files inside the root of Assets are accessible)
I tried doing flutter pub get
, but it didn’t work. How can I fix that?
2
Answers
Have you tried removing positional spaces or adding, if its incorrect, or maybe restarting your ide? if the emulator is running, try restarting that as well.
In the
initSupportFiles
function, thegetApplicationSupportDirectory
function from thepath_provider
package returns a directory path specific to the platform If you are testing the app on an emulator or simulator, these directories might not be accessible directly.you can userootBundle
to load and access them directly without relying on platform specific directories.you can update you
initSupportFiles
function with..