无法在首次构建时加载资产,但在热重启Flutter时加载

k97glaaz  于 2022-12-27  发布在  Flutter
关注(0)|答案(1)|浏览(189)

当我第一次在模拟器或真实的设备上启动应用程序八时,我得到这个错误:

flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following assertion was thrown resolving an image codec:
flutter: Unable to load asset: assets/New alert button.png
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
flutter: <asynchronous suspension>
flutter: #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:484:44)
flutter: #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:469:14)
flutter: #3      ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
flutter: #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
flutter: #5      ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
flutter: (elided 13 frames from package dart:async)
flutter:
flutter: Image provider: AssetImage(bundle: null, name: "assets/New alert button.png")
flutter: Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#3cb5b(), name: "assets/New alert
flutter:   button.png", scale: 1.0)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: Unable to load asset: assets/New route button.png
flutter: Another exception was thrown: Unable to load asset: assets/Center map button.png

阅读其他帖子相关的问题(不同的解决方案建议不同的方式)我我尝试了不同的压痕,所以我尝试了两种:

flutter:
  uses-material-design: true
  assets:
  - assets/

以及

flutter:
  uses-material-design: true
  assets:
    - assets/

但是没有任何改变。如果我正确地执行了一个热启动资产加载。我遇到的另一个问题,也许是真实的潜在问题的一个提示,是如果我从IDE将图像拖放到资产文件夹中,我会得到一个错误:

这是使用资源的一个按钮:

IconButton(
                        icon: Image.asset('assets/Center map button.png'),
                        iconSize: 60,
                        onPressed: () {
                          print('Center map button pressed');
                          _mapController.move(userLocation, 16);
                        }),

峡谷现场我做错了什么?一如既往,非常感谢你的时间和帮助。

p4rjhz4m

p4rjhz4m1#

正如注解中所建议的,问题是文件命名Center map button不接受空格分隔。通过使用camel casing centerMapButton或snake casing center_map_button,问题得到了解决。

相关问题