简体   繁体   中英

"Unable to load asset: null" in my flutter app

When my app loads, no image loads at all, and t just crashes and i keep getting this error in my flutter app but I can't seem to find the problem, usually, when it can't find load an asset it tells me which asset, but this time it just says unable to load asset null, I don't know how to find the problem

it shows a breakpoint at this point

/// 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;
  }
}

this is pub yaml file

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

This is the most common error.

It's because of spacing mistake you have in "assets:" in the pubspec.yaml

You should change the indentation spacing for assets

flutter:

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

Maintain this correct spacing:

flutter:

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

In your pubspec.yaml you need to specify every single folder you want to use. Just the folder name is enough if you want to include all the files inside. Like this:

flutter:
  uses-material-design: true

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

Make sure the two spaces indentation in YAML files.

And try to replace

await defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData());

with

await ServicesBinding.instance.defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData());

Don't forget to add:

import 'binding.dart';

Try if this works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM