我正在开发一个 flutter 插件,在使用该插件时出现 Unable to load asset
错误。使用插件时我必须做一些特别的事情吗?
我在主应用程序中加载图像没有问题。
来自 pubspec.yaml:
flutter:
# To add assets to your plugin package, add an assets section, like this:
assets:
- icons/
- icons/myimage.png # << Just to show, that this also is not not working
uses-material-design: true
plugin:
...
还试过:
- 使用 TAB 等来回移动
- 将文件夹重命名为 assets
使用图片文件夹资源:
Image.asset('icons/myimage.png', height: 12.0),
我收到这个错误:
flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following assertion was thrown resolving an image codec:
flutter: Unable to load asset: icons/myimage.png
最佳答案
要从非应用程序包加载 Assets ,您需要将 package
参数传递给加载 Assets 的方法,例如
Image.asset('icons/myimage.png', package: 'my_package', height: 12.0),
另请参阅 docs.flutter.io/flutter/widgets/Image/Image.asset.html
为了能够使用来自依赖项(插件或纯 Dart 包)的 Assets ,请遵循 https://flutter.dev/docs/development/ui/assets-and-images#bundling-of-package-assets
在依赖项中,所有文件都需要在 lib/
中,因为只有这些文件可供包用户使用。
pubspec.yaml
中的资源路径需要以packages/package_name/some_folder_inside_lib
开头
flutter
assets:
- packages/my_package/some_folder_inside_lib/my_image.png
目前还有另一个限制,所有 Assets 文件都需要在 pubspec.yaml
中单独列出,这与仅列出文件夹就足够的应用程序项目中的 Assets 相反。
点赞订阅https://github.com/flutter/flutter/issues/22944获得有关更新的通知。
关于flutter - 无法从 Flutter 插件中加载 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54907082/