在颤音中加载图像|内容root的路径不起作用,但绝对路径起作用
非常简单的应用程序,图像在项目/图像文件夹中。
- 当我尝试从相对路径加载它时: 'images/diamond.png'
图像不加载。
- 当我从绝对路径中进行操作时:'/users/macBookAir/studioprojects/secondtry_iamrich/images/dimond.png'
它做到了。
为什么这是?有人可以帮忙吗?
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
title: const Text('I am Rich'),
backgroundColor: Colors.blueGrey[900],
),
body: const Center(
child: Image(
image: AssetImage('images/diamond.png'),
),
),
),
),
);
}
这是它提供的错误:
======= Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/diamond.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:658:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/diamond.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#99cb5(), name: "images/diamond.png", scale: 1.0)
====================================================================================================
Very simple app, the image is in project/images folder.
- When I try to load it from the relative path:
'images/diamond.png'
the image doesn't load.
- When I do it from the absolute path: '/Users/MacBookAir/StudioProjects/secondtry_iamrich/images/diamond.png'
it does.
Why is this? Can anyone please help?
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
title: const Text('I am Rich'),
backgroundColor: Colors.blueGrey[900],
),
body: const Center(
child: Image(
image: AssetImage('images/diamond.png'),
),
),
),
),
);
}
This is the error it is providing:
======= Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/diamond.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:658:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/diamond.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#99cb5(), name: "images/diamond.png", scale: 1.0)
====================================================================================================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请勿忘记将图像文件夹路径添加到
pubspec..ymal
文件夹,例如在您的Android Studio中使用此插件,它将自动生成您的所有资产,无需写路径。
这样用
Do not forgot to add image folder path to your
pubspec.ymal
folder likeUse this plugin in your android studio it will auto generate your all assets no need to write path.
Use it like this
不按下来并更改您的pubspect.yaml文件中的资产部分可以修复它。
Uncomment and change the asset section in your pubspect.yaml file can fix it.