这是我的项目 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英里,但没用.我怎么才能解决这个问题呢?

推荐答案

initSupportFiles函数中,path_provider包中的getApplicationSupportDirectory函数返回特定于平台的目录路径.如果您在模拟器或模拟器上测试应用程序,这些目录可能无法直接访问.您可以使用rootBundle直接加载和访问它们,而不依赖于平台特定的目录.

您可以使用..更新您的initSupportFiles函数.

import 'package:flutter/services.dart';

Future<void> initSupportFiles() async {
  try {
    // Check if the JSON file already exists in the Application Support directory
    String jsonFilePath = 'assets/jsons/test.json';
    ByteData data = await rootBundle.load(jsonFilePath);
    final buffer = data.buffer;
    List<int> bytes = buffer.asUint8List();

    // Write the JSON file to a specific location (e.g., Application Support directory)
    // For testing purposes, you can use a different path like the Documents directory
    Directory appSupportDir = await getApplicationSupportDirectory();
    String appSupportPath = appSupportDir.path;
    String jsonFilePathInSupport = '$appSupportPath/test.json';

    File jsonFile = File(jsonFilePathInSupport);
    if (await jsonFile.exists()) {
      return;
    }
    await jsonFile.writeAsBytes(bytes);

    print('JSON file copied to Application Support directory.');
  } catch (e) {
    print('Error copying JSON file: $e');
  }
}

Flutter相关问答推荐

如何在Ffltter Chrome应用程序中使用webview_fltter_web显示自定义html而不是外部uri?

为什么要在值列表中两次放入空安全性

如何防止onTapCancel由子元素触发?

Flutter 隔离.使用多个参数运行

顶部对齐ListTile的[前导、标题、尾随]小部件

Flutter:如何设置 PersistentFooterButtons 背景?

如何动态获取任何小部件的像素?

Flutter 包错误:此应用程序使用的是已弃用的 Android 嵌入版本

添加appBar时绘制的线条发生偏移 Flu

文本溢出文本框 - Flutter

reactive_flutter_BLE - 应用找到设备但无法连接(并读取 BLE 数据)

Flutter 自定义对齐

如何让小部件根据小部件的大小构建不同的布局?

在 Flutter 中读取 Firebase 数据库数据时遇到问题

如何在 flutter 的 onChanged(更改字符)处更改 TextField 边框 colored颜色 ?

type '({bool growable}) => List' 不是类型转换中类型 'List' 的子类型

Flutter - 使用 pin 进行身份验证

如何使用 Row 和 Column 制作 L 形布局?

Flutter Firebase:如何判断返回值是否是特定对象

带图像封面的 ElevatedButton