我不能上传图像作为背景到一个容器,我已经添加了资产文件夹中的图像,并将其添加到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): ════════════════════════════════════════════════════════════════════════════════════════════════════
5条答案
按热度按时间u0sqgete1#
你需要像那样添加
字符串
nzkunb0c2#
在我的例子中,我需要package属性,因为镜像在我的library项目中,并且我正在运行example项目。
package:[您的父目录或项目名称]
我的项目名称是 'flutter_demo'
字符串
pubspect.yaml
型
目录结构:
型
2vuwiymt3#
我遇到了这个错误,这是我如何解决它:
u91tlkcl4#
试试这个:
字符串
wztqucjr5#
在我的例子中,如果你的文件在子目录中,尝试添加子目录而不仅仅是父目录。就像
字符串
而不是
型