【问题标题】:Image is not displaying in flutter图像未在颤动中显示
【发布时间】:2019-04-06 15:51:16
【问题描述】:

您好,我正在尝试在颤振中创建一个列表项。我已经创建了这样的布局,但没有显示图像。

有人知道这是什么原因吗?

import 'package:flutter/material.dart';

class CommonListItem extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _CommonListItemState();
}

class _CommonListItemState extends State<CommonListItem> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: Container(
        child: new Column(
          children: <Widget>[
            Container(
              height: 100.0,
              width: 100.0,
              child: new Image(image: AssetImage("assets/images/mountains.jpg")),
            )
          ]
        ),
      ),
    );
  }
}

pubspec.yml

name: xx
description: xx

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: 0.7.4
  firebase_auth: 0.5.18



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

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/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:
  #  - 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.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/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.io/custom-fonts/#from-packages

【问题讨论】:

  • 您是否创建了一些CommonListItems 的列表?
  • 是的,我想创建一个列表,所以我在这里创建一个列表项布局
  • 如果我使用Text,那么它就不能使用图像
  • 但是为什么你在每个项目中都使用Scaffold?你能从你的 pabsec.yaml 中粘贴资产吗?
  • @AndreyTurkovsky 好的,正确的方法是什么?我添加了 parsec.yaml

标签: android flutter


【解决方案1】:

要使用资产中的图像:

Image.asset("assets/images/source.jpg")

只需确保您还在 pubspec.yaml 中设置了资产。

【讨论】:

  • 图像从另一个文件中的资产正确加载,但在我上面的代码中没有
  • 您没有在 pubspec.yaml 中添加图像资产。它告诉你必须在里面这样做# To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg
  • 我的资产中没有这张图片assets/images/mountains.jpg 它正在加载到另一个屏幕但不在上面,我认为它是flutter的内置图像
【解决方案2】:

尝试在您的 pubsec.yaml 中执行此操作

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/

然后使用资产

【讨论】:

  • 它在另一个具有相同 pubspec 的文件中工作正常,我在另一个文件中有这个 Container( height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.white, image: DecorationImage( colorFilter: new ColorFilter.mode( Colors.black.withOpacity(0.05), BlendMode.dstATop), image: AssetImage('assets/images/mountains.jpg'), fit: BoxFit.cover, ), )
  • 问题是我的布局中的问题,我找不到。有什么想法吗?
  • 我的资产中没有这张图片assets/images/mountains.jpg 它正在加载到另一个屏幕但不在上面,我认为它是颤振的内置图像
【解决方案3】:

你从 assets 中引入了错误的 url,如果你的文件夹被称为 images 你的 url 是:images/mountains.jpg 所以你需要更改代码:

 new Image(image: AssetImage("images/mountains.jpg"))

【讨论】:

  • 它在另一个具有相同 pubspec 的文件中工作正常,我在另一个文件中有这个 Container( height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.white, image: DecorationImage( colorFilter: new ColorFilter.mode( Colors.black.withOpacity(0.05), BlendMode.dstATop), image: AssetImage('assets/images/mountains.jpg'), fit: BoxFit.cover, ), )
  • 问题是我的布局中的问题,我找不到。有什么想法吗?
  • 我的资产中没有这张图片assets/images/mountains.jpg 它正在加载到另一个屏幕但不在上面,我认为它是flutter的内置图像
【解决方案4】:
new Image(image: AssetImage("assets/images/mountains.jpg")),

使用上面的代码,请确保您已在应用程序的根目录中创建了目录(区分大小写!),例如 c:/myapp/assets/images/

然后在您的 pubspec 文件中,您需要取消注释指向资产的行

# To add assets to your application, add an assets section, like this:
   assets:
    - assets/images/*

您不必列出文件夹中的每个项目,使用星号来包含所有文件。

【讨论】:

  • 它在另一个具有相同 pubspec 的文件中工作正常,我在另一个文件中有这个 Container( height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.white, image: DecorationImage( colorFilter: new ColorFilter.mode( Colors.black.withOpacity(0.05), BlendMode.dstATop), image: AssetImage('assets/images/mountains.jpg'), fit: BoxFit.cover, ), )
  • 问题是我的布局中的问题,我找不到。有什么想法吗?
  • 我的资产中没有这张图片assets/images/mountains.jpg 它正在加载到另一个屏幕但不在上面,我认为它是颤振的内置图像
【解决方案5】:

小心 Flutter 中的选项卡。看看下面的链接。

FlutterError: Unable to load asset

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 2020-11-15
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多