FlutterError:无法加载资产 [英] FlutterError: Unable to load asset
问题描述
这是我的应用程序的文件夹结构
This is the folder structure of my app
.idea
.vscode
android
build
fonts
Oxygen-Bold.tff
Oxygen-Light.tff
Oxygen-Regular.tff
images
pizza0.png
pizza1.png
ios
lib
ui
home.dart
main.dart
test
.gitignore
.metadata
.packages
app_widgets.iml
pubspec.lock
pubspec.yaml
README.md
在我的pubspec.yaml文件中,我这样加载字体和素材资源
In my pubspec.yaml file, I load the fonts and assets like this
flutter:
uses-material-design: true
assets:
- images/pizza0.png
- images/pizza1.png
fonts:
- family: Oxygen
fonts:
- asset: fonts/Oxygen-Regular.ttf
- asset: fonts/Oxygen-Bold.ttf
weight: 700
- asset: fonts/Oxygen-Light.ttf
weight: 300
此yaml文件没有任何错误,运行"flutter包获取"给出的退出代码为0.
I'm not getting any errors for this yaml file, and running "flutter packages get" gives an exit code of 0.
在我的home.dart中,我有以下课程:
In my home.dart I have the following class:
class PizzaImageWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
AssetImage pizzaAsset = AssetImage('images/pizza0.png');
Image image = Image(image: pizzaAsset, width: 400, height: 400);
return Container(
child: image,
);
}
}
为了显示图像(省略了代码),我在其他地方使用了
Which I use elsewhere, in order to show the image (code omitted):
),
PizzaImageWidget(),
],
建筑没有错误. Flutter Doctor -v没有给出任何错误,Flutter Analyze -v也没有给出任何错误. .apk似乎构建良好,但是当我的手机上打开该应用程序时,在asset_bundle.dart中出现以下错误:
Building gives no errors. Flutter Doctor -v doesn't give any errors, neither does Flutter Analyze -v. The .apk seems to build just fine but when the app opens up on my phone I get the following error in asset_bundle.dart:
发生异常. FlutterError(无法加载资产: images/pizza0.png)
Exception has occurred. FlutterError (Unable to load asset: images/pizza0.png)
此类在asset_bundle.dart文件中引发了错误:
The error is thrown by this class in the asset_bundle.dart file:
/// An [AssetBundle] that loads resources using platform messages.
class PlatformAssetBundle extends CachingAssetBundle {
@override
Future<ByteData> load(String key) async {
final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path);
final ByteData asset =
await BinaryMessages.send('flutter/assets', encoded.buffer.asByteData());
if (asset == null)
throw FlutterError('Unable to load asset: $key');
return asset;
}
}
pizza0.png文件和pizza1.png文件都会发生这种情况.这些文件在树结构中都是可见的,在Windows资源管理器中都与VS Code中一样.字体资源加载没有问题.
This happens both for the pizza0.png file as well as the pizza1.png file. The files are visible in the tree structure, both in Windows Explorer as in VS Code. The font assets load without issue.
这是运行Flutter Run -v时得到的输出:
This is the output I am getting when running Flutter Run -v:
[+ 1068 ms] I/抖动(6489):BY图像资源引起的异常 服务╞════════════════════════════════════════════════ ════[+9 ms] I/flutter(6489):抛出了以下断言,以解决 图像编解码器:[+2毫秒] I/flutter(6489):无法加载资产: images/pizza0.png [+2毫秒] I/颤动(6489):[+1毫秒] I/颤动( 6489):引发异常时,这是堆栈:[+2毫秒] I/颤振(6489):#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)[+1毫秒] I/颤振(6489):[+1毫秒] I/颤振( 6489):#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:429:44)[+1毫秒] I/颤振(6489):[+1毫秒] I/颤振( 6489):#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:414:14)[+1毫秒] I/flutter(6489):#3 ImageProvider.resolve .. (package:flutter/src/painting/image_provider.dart:267:86)[+4毫秒] I/flutter(6489):#4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:143:20)[+3毫秒] I/flutter(6489):#5 ImageProvider.resolve. (package:flutter/src/painting/image_provider.dart:267:63)[+3毫秒] I/flutter(6489):(从dart:async包中删除了8帧)[+1 ms] I/flutter(6489):[+1 ms] I/flutter(6489):图片提供者: AssetImage(bundle:空,名称:"images/pizza0.png")[+3毫秒] I/flutter(6489):映像键:AssetBundleImageKey(bundle: PlatformAssetBundle#20fc8(),名称:"images/pizza0.png",[+1毫秒] I/颤振(6489):标度:1.0)[+2 ms] I/颤振(6489): ══════════════════════════════════════════════════ ══════════════════════════════════════════════════
[+1068 ms] I/flutter ( 6489): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════ [ +9 ms] I/flutter ( 6489): The following assertion was thrown resolving an image codec: [ +2 ms] I/flutter ( 6489): Unable to load asset: images/pizza0.png [ +2 ms] I/flutter ( 6489): [ +1 ms] I/flutter ( 6489): When the exception was thrown, this was the stack: [ +2 ms] I/flutter ( 6489): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7) [ +1 ms] I/flutter ( 6489): [ +1 ms] I/flutter ( 6489): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:429:44) [ +1 ms] I/flutter ( 6489): [ +1 ms] I/flutter ( 6489): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:414:14) [ +1 ms] I/flutter ( 6489): #3 ImageProvider.resolve.. (package:flutter/src/painting/image_provider.dart:267:86) [ +4 ms] I/flutter ( 6489): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:143:20) [ +3 ms] I/flutter ( 6489): #5 ImageProvider.resolve. (package:flutter/src/painting/image_provider.dart:267:63) [ +3 ms] I/flutter ( 6489): (elided 8 frames from package dart:async) [ +1 ms] I/flutter ( 6489): [ +1 ms] I/flutter ( 6489): Image provider: AssetImage(bundle: null, name: "images/pizza0.png") [ +3 ms] I/flutter ( 6489): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#20fc8(), name: "images/pizza0.png", [ +1 ms] I/flutter ( 6489): scale: 1.0) [ +2 ms] I/flutter ( 6489): ════════════════════════════════════════════════════════════════════════════════════════════════════
推荐答案
您应考虑assets
flutter:
assets:
- images/pizza1.png
- images/pizza0.png
更多详细信息:
flutter:
[2 whitespaces or 1 tab]assets:
[4 whitespaces or 2 tabs]- images/pizza1.png
[4 whitespaces or 2 tabs]- images/pizza0.png
这篇关于FlutterError:无法加载资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!