我如何在扑朔迷离的目录中访问AN资产子目录
我正在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Directory()
在文件系统上操作。您无法使用它访问资产,因为资产在构建过程中与该应用程序捆绑在一起。https://docs.flutter.dev/dev/deveverment/ui/资产和图像#资产捆绑
要获取应用程序中的资产列表,您可以使用未记录的生成文件
assetManifest.json
,其中包含所有捆绑资产的地图:另一个捆绑资产:另一个:选项(也许更清洁,因为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.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: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).