在颤音中加载图像|内容root的路径不起作用,但绝对路径起作用

发布于 2025-02-12 03:17:54 字数 1563 浏览 2 评论 0原文

非常简单的应用程序,图像在项目/图像文件夹中。

  1. 当我尝试从相对路径加载它时: 'images/diamond.png'

图像不加载。

  1. 当我从绝对路径中进行操作时:'/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.

  1. When I try to load it from the relative path:
    'images/diamond.png'

the image doesn't load.

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2025-02-19 03:17:54

请勿忘记将图像文件夹路径添加到pubspec..ymal文件夹,例如

  assets:
    - assets/images/
    - assets/images/icons/
    - assets/loader.json

“在此处输入图像说明”

在您的Android Studio中使用此插件,它将自动生成您的所有资产,无需写路径。

这样用

Image.asset(Assets.imagesNoDataFound)

Do not forgot to add image folder path to your pubspec.ymal folder like

  assets:
    - assets/images/
    - assets/images/icons/
    - assets/loader.json

enter image description here

Use this plugin in your android studio it will auto generate your all assets no need to write path.

Use it like this

Image.asset(Assets.imagesNoDataFound)
违心° 2025-02-19 03:17:54

不按下来并更改您的pubspect.yaml文件中的资产部分可以修复它。

  assets:
    - images/

Uncomment and change the asset section in your pubspect.yaml file can fix it.

  assets:
    - images/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文