这是我的项目 struct :
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
|-- ...
这是我的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:我无法访问资源内的子文件夹中的任何文件(只能访问资源根目录内的文件)
我试着开到flutter pub get
英里,但没用.我怎么才能解决这个问题呢?