dart Flutter:无法加载资源

zaqlnxep  于 2023-07-31  发布在  Flutter
关注(0)|答案(5)|浏览(132)

我不能上传图像作为背景到一个容器,我已经添加了资产文件夹中的图像,并将其添加到pubspec.yaml和显示我的错误:

I/flutter ( 6664): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 6664): The following assertion was thrown resolving an image codec:
I/flutter ( 6664): Unable to load asset: assets/images/img.png
I/flutter ( 6664): 
I/flutter ( 6664): When the exception was thrown, this was the stack:
I/flutter ( 6664): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 6664): <asynchronous suspension>
I/flutter ( 6664): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:484:44)
I/flutter ( 6664): #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:469:14)
I/flutter ( 6664): #3      ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
I/flutter ( 6664): #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter ( 6664): #5      ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
I/flutter ( 6664): (elided 13 frames from package dart:async)
I/flutter ( 6664): 
I/flutter ( 6664): Image provider: AssetImage(bundle: null, name: "assets/images/img.png")
I/flutter ( 6664): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#99877(), name: "assets/images/img.png",
I/flutter ( 6664):   scale: 1.0)
I/flutter ( 6664): ════════════════════════════════════════════════════════════════════════════════════════════════════

字符串
代码:

body: Form(
          child: Column(
            children: <Widget>[
               Container(
                 decoration: BoxDecoration(
                   image: DecorationImage(
                     image: AssetImage("assets/images/img.png"),
                     fit: BoxFit.cover,
                   ),
                 ),


我已添加

assets:
  - assets/images/


到pubspec.yaml
图像文件夹


的数据
而在

assets:
  - assets/images/img.png
I/flutter ( 6664): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 6664): The following assertion was thrown resolving an image codec:
I/flutter ( 6664): Unable to load asset: assets/images/img.png
I/flutter ( 6664): 
I/flutter ( 6664): When the exception was thrown, this was the stack:
I/flutter ( 6664): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 6664): <asynchronous suspension>
I/flutter ( 6664): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:484:44)
I/flutter ( 6664): #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:469:14)
I/flutter ( 6664): #3      ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
I/flutter ( 6664): #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter ( 6664): #5      ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
I/flutter ( 6664): (elided 13 frames from package dart:async)
I/flutter ( 6664): 
I/flutter ( 6664): Image provider: AssetImage(bundle: null, name: "assets/images/img.png")
I/flutter ( 6664): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#c1182(), name: "assets/images/img.png",
I/flutter ( 6664):   scale: 1.0)
I/flutter ( 6664): ════════════════════════════════════════════════════════════════════════════════════════════════════
u0sqgete

u0sqgete1#

你需要像那样添加

flutter:
  assets:
    - assets/images/img.png

字符串

nzkunb0c

nzkunb0c2#

在我的例子中,我需要package属性,因为镜像在我的library项目中,并且我正在运行example项目。

package:[您的父目录或项目名称]

我的项目名称是 'flutter_demo'

AssetImage('assets/images/ic_welcome.png', package: 'flutter_demo'),

字符串

pubspect.yaml

flutter:
  assets:
    - assets/images/

目录结构:

flutter_demo
  -- assets
    -- images
      -- ic_welcome.png

2vuwiymt

2vuwiymt3#

我遇到了这个错误,这是我如何解决它:

  • 首先确保assets文件夹位于项目的根目录下,lib文件夹之外。
  • 检查AssetImage()中的图像路径字符串前面是否有斜杠。
  • 检查pubspec.yaml和AssetImage中的文件名和路径是否正确。
  • 始终记住保存文件并在进行更改后进行热重新加载。
  • 对于svg文件,请使用flutter_svg package
u91tlkcl

u91tlkcl4#

试试这个:

flutter:
  assets:
    -android/assests/directory/

字符串

wztqucjr

wztqucjr5#

在我的例子中,如果你的文件在子目录中,尝试添加子目录而不仅仅是父目录。就像

flutter:
  assets:
    - assets/subdirectory1/
    - assets/subdirectory2/

字符串
而不是

flutter:
  assets:
    - assets/

相关问题