Flutter 无法在物理设备上加载图像资产(但在模拟器上加载它就好了)
Posted
技术标签:
【中文标题】Flutter 无法在物理设备上加载图像资产(但在模拟器上加载它就好了)【英文标题】:Flutter unable to load image asset on physical device (but loading it just fine on the emulator) 【发布时间】:2020-02-08 15:52:31 【问题描述】:我正在构建一个颤振应用程序,当我在手机上运行该应用程序时不会加载资产图像,但当我在模拟器上运行该应用程序时它加载正常。
这是我得到的错误:
> 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'),
),
),
),
),
);
非常令人困惑的是为什么图像在模拟器上正确加载,而不是在物理设备上。有什么线索吗?
非常感谢。
【问题讨论】:
【参考方案1】:逐步执行此操作,这应该可以正常工作。基本上你做错了。
把图片放到assets文件夹,所以根是
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
用户界面代码:
//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'),
)
)
我相信你现在就能得到图像了。让我知道这是否有帮助。
【讨论】:
你好@Alok!我可以确认,如果我在 pubspec.yaml 中指定图像的整个路径,它甚至可以在物理设备上正确呈现!非常感谢您的提示。顺便说一句,如果您像我以前那样指定资产文件夹名称,难道不应该获取所有文件吗?这就是它在文档上的内容... 嗯,准确来说,设备通常不会确认从您的文件夹中获取文件,您还需要指定文件夹的名称assets
,这是一个很好的做法,并且不会让您感到困惑,也不会导致设备混乱。这里assets:
,需要在路径中指定数据,你可以试试,不知道行不行,assets: images/diamond.png
,其余的保持不变。以上是关于Flutter 无法在物理设备上加载图像资产(但在模拟器上加载它就好了)的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:无法加载资产图像提供者:AssetImage(bundle:null,名称:“assets/images/rose.jpg”)