我创建了一个flutter包,图像在images文件夹中。
flutter:
uses-material-design: true
# To add assets to your package, add an assets section, like this:
# assets:
assets:
- images/
- images/location_pointer.png
我正在尝试加载此图像:
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
我创建了一个项目,并在其中导入了一个包:
dependencies:
flutter:
sdk: flutter
abc_pkg:
path: /Users/mosh/Documents/flutter proj/abcPackage/abc_pkg
我可以在运行此项目时加载包,但无法加载包中的图像。
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/location_pointer.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/location_pointer.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#092c6(), name: "images/location_pointer.png", scale: 1.0)
====================================================================================================
-
确保所有图像都保存在名为"images"的目录中,该目录位于名为"assets"的目录内。
-
更新您的pubspec.yaml如下:
flutter:
uses-material-design: true
# To add assets to your package, add an assets section, like this:
# assets:
assets:
- assets/images/location_pointer.png
- 按如下方式更新您的dart文件:
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'assets/images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
请尝试此代码。并在lib文件夹所在的目录
在您的pubspec.yaml文件中
flutter:
uses-material-design: true
assets:
- images/
和您的dart文件
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
如果您确定您确实在pubspec.yaml文件中添加了资产,只需将包的名称添加到image.asset的包字段中如下所示:
Image.asset(
e.flagUri!,
width: 40,
fit: BoxFit.fitWidth,
package: 'igmu_package',// this is pacakge name
)