小编典典

FlutterError:无法加载资产

flutter

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

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

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 package get”给出的退出代码为0。

在我的home.dart中,我有以下课程:

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中出现以下错误:

发生异常。FlutterError(无法加载资源: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时得到的输出:

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):(从dart:async软件包中删除了8帧)[+1 ms] I / flutter(6489) :[+1毫秒] I /
flutter(6489):图片提供者:AssetImage(bundle:null,名称:“ images / pizza0.png”)[+3毫秒]
I / flutter(6489):图片密钥:AssetBundleImageKey(bundle
:PlatformAssetBundle#20fc8(),名称:“ images / pizza0.png”,[+1毫秒] I /
Flutter(6489):比例尺:1.0)[+2毫秒] I / flutter(6489):


阅读 663

收藏
2020-08-13

共1个答案

小编典典

您应该考虑缩进 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
2020-08-13