gpt4 book ai didi

flutter 错误 : Unable to load asset

转载 作者:IT老高 更新时间:2023-10-28 12:30:41 27 4
gpt4 key购买 nike

这是我的应用程序的文件夹结构

.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 文件中,我像这样加载字体和 Assets

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

我没有收到此 pubspec.yaml 的任何错误,并且运行 flutter packages get 给出的退出代码为 0。

在我的 home.dart 中,我有以下类(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,
);
}
}

我在其他地方使用的,为了显示图像(代码省略):

        ),
PizzaImageWidget(),
],

该建筑没有错误。 Flutter Doctor -v 不会给出任何错误,Flutter Analyze -v 也不会。 .apk 似乎构建得很好,但是当应用程序在我的手机上打开时,我在asset_bundle.dart 中收到以下错误:

Exception has occurred. FlutterError (Unable to load asset:images/pizza0.png)

这个类在asset_bundle.dart文件中抛出的错误:

/// 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 中。字体资源加载没有问题。

这是我在运行 Flutter Run -v 时得到的输出:

[+1068 ms] I/flutter ( 6489): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCESERVICE ╞════════════════════════════════════════════════════ [ +9ms] I/flutter ( 6489): The following assertion was thrown resolving animage 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) [ +1ms] 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

完成这一切后,您可以进行热重启。

关于 flutter 错误 : Unable to load asset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53659809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com