无法在颤动中加载资产(图像)

时间:2021-07-28 18:00:49

标签: android flutter

This is how the code (app) looks like.

大家这是我第一次这样做,所以我会尽量做到准确和整洁......

所以我刚刚开始学习颤振,我正在观看有关它的课程,但是在复制讲师正在输入的内容时,每当我尝试在我的虚拟或物理设备上运行此代码时,我都偶然发现了这个问题” t 加载图像。我检查了 pubsec.yaml 文件,一切似乎都运行良好,但图像无法加载。顺便说一句,我正在使用最新版本的 AndroidStudio(我猜是金丝雀)。在此先感谢我希望有人能帮助我。代码如下:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          title: Center(
            child: Text('I am rich.'),
          ),
          backgroundColor: Colors.blueGrey[900],
        ),
        body: Center(
          child: Image(
            image: AssetImage('diamond.png'),
          ),
        ),
      ),
    ),
  );
}

这里是代码中图片的下载链接: https://drive.google.com/uc?export=download&id=1IA7othwL6jGtvc1Wfyj0K6hxrEH9OHam

2 个答案:

答案 0 :(得分:0)

当您有要在代码中使用的本地文件时,您需要记住将其添加到资产部分下的 pubspec.yaml 文件中。这是我在那里注册几张图片的示例:

flutter:
  ...
  assets:
    - diamond.png
    - gemstones/emerald.jpg

请注意,包含您要使用的资产的文件(和文件夹)应注册在您应用的根文件夹中,而不是您的 lib 文件夹中。

答案 1 :(得分:0)

您是否将图像的路径放在 pubspec.yaml 中? 为了让 Flutter 识别您的资产图片,您需要将他的路径放在您项目的 pubspec.yaml 文件中。