图像未在Flutter应用中显示。 但是我在调试控制台中遇到了一些错误。
I/FlutterActivityDelegate(22603): onResume setting current activity to this
I/FlutterActivityDelegate(22603): onResume setting current activity to this
I/Timeline(22603): Timeline: Activity_idle id: android.os.BinderProxy@3eb59326 time:39937973
I/flutter (22603): ══╡ EXCEPTION CAUGHT BY SERVICES ╞══════════════════════════════════════════════════════════════════
I/flutter (22603): The following assertion was thrown resolving an image codec:
I/flutter (22603): Unable to load asset: assets/images/logo.png
I/flutter (22603): When the exception was thrown, this was the stack:
I/flutter (22603): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (22603): <asynchronous suspension>
I/flutter (22603): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:383:44)
I/flutter (22603): <asynchronous suspension>
I/flutter (22603): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:368:14)
I/flutter (22603): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:265:86)
I/flutter (22603): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:82:22)
I/flutter (22603): #5 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:265:63)
I/flutter (22603): (elided 8 frames from package dart:async)
I/flutter (22603): Image provider: AssetImage(bundle: null, name: "assets/images/logo.png")
I/flutter (22603): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#19ce7(), name: "assets/images/logo.png",
I/flutter (22603): scale: 1.0)
I/flutter (22603): ════════════════════════════════════════════════════════════════════════════════════════════════════
D/ViewRootImpl(22603): ViewPostImeInputStage ACTION_DOWN
D/ViewRootImpl(22603): ViewPostImeInputStage ACTION_DOWN
我的Pubspec.yaml文件
资产: -asset / images / logo.png
login.dart代码
new Image.asset(“ assets / images / logo.png”,宽度:60.0, 高度:24.0, 适合:BoxFit.cover)
答案 0 :(得分:5)
我以错误的方式提到了图像文件。我在'-'和图片名称之间放置空格,而不是Tab。
资产:
- assets/images/logo.png
在pubspec.yaml文件中的字符之间而不是制表符之间不要留空格
答案 1 :(得分:5)
确保您在 Image.asset 中引用的文件夹包含该文件。
例如:
Image.asset(
"./assets/images/loading.gif",
height: 125.0,
width: 125.0,
)
文件夹应该是:
C:\your_app_folder\assets\images
pubspec.yaml:
assets:
- assets/
- assets/images/
运行 flutter clean 清理中间文件并刷新。
答案 2 :(得分:1)
可能有两个问题:
1。)要么pubspec.ymal文件没有适当的缩进。随附摘要以供参考。
flutter:
uses-material-design: true
assets:
- assets/
-assets/
将考虑目录中的所有图像。
2。)。如果您使用的是.jpg图片,则无论您在哪里调用,都请将其更改为.jpeg。
附上代码段供您参考
class _UserLoginState extends State<UserLogin> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(
children: <Widget>[
new Image(image: new AssetImage("assets/christmas.jpeg"),
fit: BoxFit.cover,)
],
)
);
}
}
答案 3 :(得分:0)
我假设您将文件复制到assets/images/
文件夹中了吗?
另外,您需要将图像引用到pubspec.yaml
文件中。
flutter:
...
assets:
- assets/images/logo.png
答案 4 :(得分:0)
确保图像文件夹位于项目文件夹中。
答案 5 :(得分:0)
引用https://api.flutter.dev/flutter/painting/AssetImage-class.html
在pubspec.yaml文件中添加资产图像,并在资产中提及图像的路径,并且效果很好。
new AssetImage("images/logo.png")
例如
CircleAvatar(
radius: 80,
backgroundImage: AssetImage('images/logo.png'),
),
答案 6 :(得分:0)
我刚替换
Image.asset("assets\images\_Lightweight.jpeg"),
收件人
Image.asset("assets/images/_Lightweight.jpeg"),
答案 7 :(得分:0)
我的问题是资产缩进部分。我在行的开头写了它,但是应该在flutter:节之后用Tab缩进。
罗比
答案 8 :(得分:0)
答案 9 :(得分:0)
将新图像添加到资产文件夹后,我遇到了同样的问题。我正在执行“热重载”,我从后台销毁了应用程序,重新运行应用程序,问题得到解决。