首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在我的flutter应用中出现"Unable to load asset: null“

在我的flutter应用中出现"Unable to load asset: null“
EN

Stack Overflow用户
提问于 2020-11-16 13:04:37
回答 2查看 469关注 0票数 0

当我的应用程序加载时,根本没有图像加载,而且不会崩溃,我在flutter应用程序中不断收到这个错误,但我似乎找不到问题,通常,当它找不到加载资产时,它会告诉我是哪个资产,但这次它只是说无法加载资产null,我不知道如何找到问题

它在这一点显示一个断点。

代码语言:javascript
运行
复制
/// 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 defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData());
    if (asset == null)
      throw FlutterError('Unable to load asset: $key');   ******RIGHT HERE IS THE BREAKPOINT
    return asset;
  }
}

这是pub yaml文件

代码语言:javascript
运行
复制
name: wepay
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies: 
  flutter:
    sdk: flutter
    


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.0

dev_dependencies: 
  flutter_test:
    sdk: flutter
  
  flutter_launcher_icons: "^0.8.0"
  page_indicator: ^0.3.0
  twitter_qr_scanner: any
  audioplayers: ^0.16.2
  modal_bottom_sheet: 0.2.2 
  overlay_screen: ^1.2.0+1 
  show_up_animation: ^1.0.4
  flutter_spinkit: "^4.1.2"
  auto_size_text: ^2.1.0
  cloud_firestore: ^0.14.0 
  firebase_core : ^0.5.0
  qr_flutter: ^3.2.0
  intl: ^0.16.1
  secure_random: ^1.0.0 
  fluttertoast: ^7.0.1
  clipboard: ^0.1.2+8
  social_share: ^2.0.5


 










    
flutter_icons:
  android: "icon"
  ios: true
  image_path: "assets/images/icon.png"


 

  
  

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  assets: 
   
    - assets/images/
    - assets/sounds/scan.mp3
    
    

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  
  fonts:
    - family: Montserrat
      fonts:
        - asset: assets/fonts/Montserrat-Black.ttf
          weight: 900
        - asset: assets/fonts/Montserrat-Bold.ttf
          weight: 700
        - asset: assets/fonts/Montserrat-ExtraBold.ttf
          weight: 800
        - asset: assets/fonts/Montserrat-Medium.ttf
          weight: 500
        - asset: assets/fonts/Montserrat-Regular.ttf
          weight: 400
        - asset: assets/fonts/Montserrat-SemiBold.ttf
          weight: 600
    - family: Roboto
      fonts:
        - asset: assets/fonts/Roboto-Regular.ttf
        - asset: assets/fonts/Roboto-Bold.ttf
        - asset: assets/fonts/Roboto-Light.ttf
    



 

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages
EN

回答 2

Stack Overflow用户

发布于 2020-11-16 13:55:14

这是最常见的错误。

这是因为您在pubspec.yaml中的"assets:“中存在空格错误

您应该更改资源的缩进间距

代码语言:javascript
运行
复制
flutter:

  assets:
    - images/apple.png
    - images/apple1.png

保持此正确的间距:

代码语言:javascript
运行
复制
flutter:

[2 whitespaces or 1 tab]assets:
[3 whitespaces]- images/pizza1.png
[3 whitespaces]- images/pizza0.png
票数 0
EN

Stack Overflow用户

发布于 2020-11-16 14:20:07

在您的pubspec.yaml中,您需要指定要使用的每个文件夹。如果您想要包含其中的所有文件,仅文件夹名称就足够了。如下所示:

代码语言:javascript
运行
复制
flutter:
  uses-material-design: true

  assets: 
    - assets/
    - assets/images/
    - assets/sounds/

确保YAML文件中的两个空格缩进。

并尝试替换掉

代码语言:javascript
运行
复制
await defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData());

使用

代码语言:javascript
运行
复制
await ServicesBinding.instance.defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData());

别忘了添加:

代码语言:javascript
运行
复制
import 'binding.dart';

如果这对您有效,请尝试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64852920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档