简体   繁体   English

无法在 flutter 中加载图像资产

[英]Unable to load image asset in flutter

Hi I have been trying to load this image as my background but whenever I try to do debug and run my app this exception occurs.嗨,我一直在尝试将此图像加载为我的背景,但每当我尝试调试和运行我的应用程序时,就会发生此异常。 Down Below I have tried indentation but still, the issue comes when I try to run the image_provide.dart file opens and gives this error in debug console this app is a login UI please clear this.在下面我尝试过缩进,但是当我尝试运行 image_provide.dart 文件打开并在调试控制台中出现此错误时,问题仍然存在,此应用程序是登录 UI,请清除此错误。 I am flutter beginner so criticism is wholeheartedly accepted我是 flutter 初学者所以全心接受批评


════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assetimagesclock.png

When the exception was thrown, this was the stack
#0      PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:227
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:675
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "assetimagesclock.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9c9b1(), name: "assetimagesclock.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════


this is my main.dart这是我的主要内容。dart

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(home: new MyApp()),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
            child: Column(
          children: <Widget>[
            Container(
              height: 400,
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('asset\images\clock.png'),
                    fit: BoxFit.fill),
              ),
            )
          ],
        )),
      ),
    );
  }
}
 

this is my pubspec.yaml file这是我的 pubspec.yaml 文件



name: login_ui
description: A new Flutter project.

# 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
  simple_animations: ^1.3.3

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

dev_dependencies:
  flutter_test:
    sdk: flutter


# 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

  # To add assets to your application, add an assets section, like this:
  assets:
   - assets/images/
  #  - 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


I think you need to indent properly with 2 spaces the assets in your pubspec.yaml .我认为您需要使用pubspec.yaml中的资产正确缩进 2 个空格。

  assets:
    - assets/images/

I think your pubspec.yaml seems to be just working fine.我认为您的 pubspec.yaml 似乎工作正常。 But you've made a typo at this following line: AssetImage(' asset \images\clock.png'), Replace it by: AssetImage(' assets \images\clock.png'),但是你在下面这行打错了:AssetImage(' asset \images\clock.png'),替换为:AssetImage(' assets \images\clock.png'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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