小部件中的Image.asset

时间:2019-09-04 15:21:21

标签: flutter dart

我想在小部件中插入图像,但始终在控制台中出现错误,我不知道该怎么做。

class Menue extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Color.fromRGBO(26, 26, 26, 100),
      body: new Container(
        child: new Center(
          child: new Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset("Q-Extreme Menue.png"),
                  new IconButton(onPressed: () {}, icon: new Icon(null),),

                ],
                ),
                ....

I / flutter(8295):抛出了以下断言来解析图像编解码器: I / flutter(8295):无法加载资产:Q-Extreme Menue.png I /颤振(8295): I / flutter(8295):引发异常时,这是堆栈: I / flutter(8295):#0 PlatformAssetBundle.load(package:flutter / src / services / asset_bundle.dart:221:7) I /颤振(8295): I / flutter(8295):#1 AssetBundleImageProvider._loadAsync(package:flutter / src / painting / image_provider.dart:464:44) I /颤振(8295): I / flutter(8295):#2 AssetBundleImageProvider.load(包:flutter / src / painting / image_provider.dart:449:14) I / flutter(8295):#3 ImageProvider.resolve ...(package:flutter / src / painting / image_provider.dart:316:48) I / flutter(8295):#4 ImageCache.putIfAbsent(包:flutter / src / painting / image_cache.dart:160:22) I / flutter(8295):#5 ImageProvider.resolve ..(package:flutter / src / painting / image_provider.dart:316:25) I / flutter(8295):(从dart:async包中删除了13帧) I /颤振(8295): I / flutter(8295):图片提供者:AssetImage(捆绑:null,名称:“ Q-Extreme Menue.png”) I / flutter(8295):图像键:AssetBundleImageKey(束:PlatformAssetBundle#88838(),名称:“ Q-Extreme Menue.png”, I / flutter(8295):比例:1.0)



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:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
assets:
  - assets/images/Q-Extreme Menue.png
  - Q-Extreme Menue.png


2 个答案:

答案 0 :(得分:1)

将图片放置在如图所示的文件夹(assets/images)中:

image folder

然后,您可以在pubspec.yaml中引用资产(请确保删除文件名中的空格;可以将其命名为Q_Extreme_Menue.png

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:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  assets:
    - assets/images/Q_Extreme_Menue.png

现在使用资产时,您将提供完整路径:

Image.asset('assets/images/Q_Extreme_Menue.png'),

答案 1 :(得分:0)

我认为您在缩进空格中犯了错误。

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:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  assets:
    - assets/images/Q-Extreme Menue.png