我如何在扑朔迷离的目录中访问AN资产子目录

发布于 2025-02-09 06:09:19 字数 385 浏览 0 评论 0原文

我正在flutter中的一个应用程序,在那里我必须找到从目录中的特定关键字开始的文件名(然后随后使用这些文件名进行操作)。但是,我在访问目录本身时遇到麻烦。

正如许多在线资源所建议的那样,我的项目的根目录中有一个“资产”文件夹,该目录中的子目录。我将此子目录在我的pubspec.yaml中包含在“资产/[子目录名称]'的格式中,通常可以使用“资产/[子目录名称]/[fileName]'访问其内容,并采用诸如loadString之类的方法。但是,当我尝试访问这样的目录时:目录('Assets/subdirectory/')时,FileSystemexception不会抛出此类目录错误。我尝试了添加'./'或删除最终斜线的通常组合,但无济于事。

目录filepath是相对于其他事物的吗?有更好的方法吗?任何帮助将不胜感激。

I am working on an app in Flutter where I will have to find filenames that start with specific keywords in a directory (and then do things with those filenames subsequently). However, I am encountering trouble accessing the directory itself.

I have an 'assets' folder in the root directory of my project, as many online sources recommended, and a subdirectory in that directory. I include this subdirectory in my pubspec.yaml in the format 'assets/[subdirectory name]', and can generally access its contents with 'assets/[subdirectory name]/[filename]' with methods like loadString. However, when I try to access the directory like this: Directory('assets/subdirectory/'), a FileSystemException no such directory error is thrown. I have tried the usual combination of adding './' or removing the final slash, to no avail.

Are Directory filepaths relative to something else? Is there a better way to do this? Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2025-02-16 06:09:19

Directory()在文件系统上操作。您无法使用它访问资产,因为资产在构建过程中与该应用程序捆绑在一起。

在构建过程中,将资产放入一个特殊的档案中,称为“资产捆绑包”,该资产包在运行时读取。

https://docs.flutter.dev/dev/deveverment/ui/资产和图像#资产捆绑

要获取应用程序中的资产列表,您可以使用未记录的生成文件assetManifest.json,其中包含所有捆绑资产的地图:另一个捆绑资产:

final assetsManifest = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final assets = json.decode(assetsManifest).keys;

另一个:选项(也许更清洁,因为AssetsManifest.json确实是无证的,并且可能会在将来的版本中更改)就是简单地制作您自己的资产列表并将其保存为另一个资产(JSON文件)。

Directory() operates on the filesystem. You can't access assets with it because assets are bundled with the app during the build process.

During a build, Flutter places assets into a special archive called the asset bundle that apps read from at runtime.

https://docs.flutter.dev/development/ui/assets-and-images#asset-bundling

To get a list of assets in the app, you can use the undocumented generated file AssetManifest.json which contains a map of all bundled assets:

final assetsManifest = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final assets = json.decode(assetsManifest).keys;

Another option (perhaps cleaner, since AssetsManifest.json is AFAIK indeed undocumented and might change in future versions) would be to simply make your own list of assets and save it as another asset (JSON file).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文