这是我第一次使用 flutter,遵循文档中描述的所有内容并向小部件添加图像。但我收到此错误,我不明白出了什么问题
Am tried this way
@override
Widget build(BuildContext context) {
return Container(
child: Image.asset("drawables/ic_launcher_transparent.png", width : 100, height: 100),
);
}
- 文件结构
|-- 库
|--- 可绘制
|----- ic_launcher_transparent.png
- pubspec.yaml
使用 Material 设计:true
Assets :
- 绘图
Am getting this error
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: drawables/ic_launcher_transparent.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:668:31)
#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:651:14)
#3 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:504:13)
...
Image provider: AssetImage(bundle: null, name: "drawables/ic_launcher_transparent.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#59d28(), name: "drawables/ic_launcher_transparent.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
最佳答案
该文件位于 lib 文件夹内。所以你需要在url中链接lib。检查下面。
@override
Widget build(BuildContext context) {
return Container(
child: Image.asset("lib/drawables/ic_launcher_transparent.png", width : 100, height: 100),
);
}
Pubspec 将如下所示。
- pubspec.yaml
使用 Material 设计:true
Assets :
- lib/drawables/
关于image - flutter : Unable to load image asset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64158543/