[英]Flutter unable to load image asset on physical device (but loading it just fine on the emulator)
I am building a flutter app and an asset image won't load when I run the app on my phone, but it loads fine when I run the app on the emulator.我正在构建一个 flutter 应用程序,当我在手机上运行该应用程序时不会加载资产图像,但是当我在模拟器上运行该应用程序时它可以正常加载。
Here's the error I get:这是我得到的错误:
> 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):
> ════════════════════════════════════════════════════════════════════════════════════════════════════
And here's my pubspec.yaml这是我的 pubspec.yaml
flutter:
uses-material-design: true
assets:
- images/
Here's main.dart:这里是main.dart:
import 'package:flutter/material.dart';导入“包:颤振/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'),
),
),
),
),
);
}
What's very confusing is why the image is loading correctly on the emulator and not on the physical device.非常令人困惑的是为什么图像在模拟器上正确加载而不是在物理设备上。 Any clue?有什么线索吗?
Thanks a lot.非常感谢。
Do this step by step, and this should work just fine.逐步执行此操作,这应该可以正常工作。 Basically you're doing it in wrong way.基本上你做错了。
- Put the images in the assets folder, so the root is
assets/diamond.png
将图片放在 assets 文件夹中,所以根目录是assets/diamond.png
- Or if you have another folder in your assets folder,
images
, then the root isassets/images/diamond.png
或者,如果您的 assets 文件夹中有另一个文件夹images
,那么根目录是assets/images/diamond.png
pubspec.yaml 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 Code:用户界面代码:
//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'),
)
)
I am sure you will be able to get the image now.我相信你现在就能得到图像。 Let me know if that helps.让我知道这是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.