Flutter不会加载特定图像

2wnc66cl  于 2022-11-17  发布在  Flutter
关注(0)|答案(2)|浏览(120)

当我尝试从本地(位于assets/images/)的Unsplash加载this image时,没有显示任何内容,也没有报告错误。当我将图像源更改为network时,图像加载完全正常(但速度较慢)。
不工作:

Container(
     decoration: BoxDecoration(
          image: DecorationImage(
             image: AssetImage("assets/images/image.jpg"),
             fit: BoxFit.cover))

作品:

Container(
     decoration: BoxDecoration(
         image: DecorationImage(
             image: NetworkImage("https://source.unsplash.com/zuueig1w8WI/"),
              fit: BoxFit.cover)),

当前正在使用Flutter Web。

编辑图像已在pubspec中。

name: website
description: Wesbite

# 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.
version: 2.0.0+1

environment:
    sdk: ">=2.7.0 <3.0.0"

dependencies:
    flutter:
        sdk: flutter
    auto_size_text: ^2.1.0
    cached_network_image: ^2.5.0
    firebase_analytics: ^6.3.0
    font_awesome_flutter: ^8.11.0
    google_fonts: ^1.1.1
    http: ^0.12.2
    provider: ^4.3.2+3
    url_launcher: ^5.7.10
    velocity_x: ^1.3.1

dev_dependencies:
    flutter_test:
        sdk: flutter
# The following section is specific to Flutter.
flutter:
    assets:
        - assets/images/
        - assets/licenses/

    # 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
ujv3wf0j

ujv3wf0j1#

请检查assets文件夹中的文件扩展名。
源图像是.jfif,虽然兼容,但可能意味着flutter无法找到该文件,因为它在查找image.jpg

wtlkbnrh

wtlkbnrh2#

检查您的pubspec.yaml是否包含了资产路径。如果您没有包含路径,请执行以下操作。

**EDIT:**您不必添加“pubspec. yaml”上的每个图像路径,只需添加主图像路径,如下所示:

资产/图像/

相关问题