我正在构建一个颤动的应用程序,并且在手机上运行该应用程序时不会加载资产图片,但是当我在模拟器上运行该应用程序时,该资源图片可以正常加载。
这是我得到的错误:
> I/flutter (26364): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE
> ╞════════════════════════════════════════════════════ I/flutter
> (26364): The following assertion was thrown resolving an image codec:
> I/flutter (26364): Unable to load asset: images/diamond.png I/flutter
> (26364): I/flutter (26364): When the exception was thrown, this was
> the stack: I/flutter (26364): #0 PlatformAssetBundle.load
> (package:flutter/src/services/asset_bundle.dart:221:7) I/flutter
> (26364): <asynchronous suspension> I/flutter (26364): #1
> AssetBundleImageProvider._loadAsync
> (package:flutter/src/painting/image_provider.dart:464:44) I/flutter
> (26364): <asynchronous suspension> I/flutter (26364): #2
> AssetBundleImageProvider.load
> (package:flutter/src/painting/image_provider.dart:449:14) I/flutter
> (26364): #3 ImageProvider.resolve.<anonymous closure>.<anonymous
> closure>.<anonymous closure>
> (package:flutter/src/painting/image_provider.dart:316:48) I/flutter
> (26364): #4 ImageCache.putIfAbsent
> (package:flutter/src/painting/image_cache.dart:160:22) I/flutter
> (26364): #5 ImageProvider.resolve.<anonymous closure>.<anonymous
> closure> (package:flutter/src/painting/image_provider.dart:316:25)
> I/flutter (26364): (elided 13 frames from package dart:async)
> I/flutter (26364): I/flutter (26364): Image provider:
> AssetImage(bundle: null, name: "images/diamond.png") I/flutter
> (26364): Image key: AssetBundleImageKey(bundle:
> PlatformAssetBundle#5b025(), name: "images/diamond.png", I/flutter
> (26364): scale: 1.0) I/flutter (26364):
> ════════════════════════════════════════════════════════════════════════════════════════════════════
这是我的pubspec.yaml
flutter:
uses-material-design: true
assets:
- images/
这是main.dart:
导入“ package:flutter / material.dart”;
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueGrey[900],
title: Text('Picture Motivation'),
),
body: Center(
child: Image(
image: AssetImage('images/diamond.png'),
),
),
),
),
);
}
非常令人困惑的是,为什么映像正确地加载在模拟器上而不是物理设备上。有任何线索吗?
非常感谢。
答案 0 :(得分:1)
逐步执行此操作,这应该可以正常工作。基本上,您用错误的方式进行操作。
- 将图像放在资产文件夹中,所以根为
assets/diamond.png
- 或者,如果资产文件夹中还有另一个文件夹
images
,则根目录为assets/images/diamond.png
pubspec.yaml
flutter:
uses-material-design: true
//you have to give the proper path in order to get the image in your UI
assets:
- assets/images/diamond.png
UI代码:
//this is how your image calling work, you just have to copy the path, same as pubspec.yaml
Center(
child: Image(
image: AssetImage('assets/images/diamond.png'),
)
)
我确信您现在将能够获得图像。让我知道是否有帮助。