我无法将图像作为背景上传到容器,我已将图像添加到Assets文件夹中,并将其添加到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): ════════════════════════════════════════════════════════════════════════════════════════════════════
答案 0 :(得分:5)
您需要添加
flutter:
assets:
- assets/images/img.png
答案 1 :(得分:2)
就我而言,我需要 package 属性,因为该图像位于我的 library 项目中,并且正在运行 example 项目。
软件包:[您的父目录或项目名称]
例如我的项目名称是'flutter_demo'
AssetImage('assets/images/ic_welcome.png', package: 'flutter_demo'),
pubspect.yaml
flutter:
assets:
- assets/images/
目录结构:
flutter_demo
-- assets
-- images
-- ic_welcome.png
答案 2 :(得分:0)
请不要使用反斜杠,如果有人试图在 Windows 外编译您的项目,它可能会失败。
此外,也无法保证未来的提交会拒绝反斜杠或将它们视为转义字符序列。那么你自己的项目将无法在任何地方编译。
pubspec.yaml
查找缩进,它需要 2 个空格,并确保在 flutter 后将资产放置 2 个空格
如果您希望包含文件夹 assets/files/,则需要 - assets/files/
,而不是 - assets/
,也不是 - assets
,也不是 -assets/files
。同样适用于您需要的图标 - assets/images/icons/
在 flutter 的 pubsec.yaml
文件中,您需要从 uses-material-design 的同一列位置开始写行 assets: ...... : true 行开始于。
你的 pubspect.yaml
文件结构应该是这样的
flutter:
uses-material-design: true
assets:
- images/some_image.png
- images/some_icon.png
毕竟这命中了一个 flutter clean
命令