【问题标题】:Image not displaying in flutter from assets图像未显示在资产中
【发布时间】:2021-10-26 16:29:13
【问题描述】:

以下代码中的嗨图片未从资产文件夹中显示。我添加了 pubspec 也没有显示静止图像。

谁能帮帮我我是新来的颤振

import 'dart:async';

import 'package:flutter/material.dart';

import 'login_screen.dart';

class SplashScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Timer(Duration(seconds: 5), () {
      Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(
              builder: (context) => LoginScreen(
                    name: "Sai Gopi",
                  )),
          (route) => false);
    });
    return Scaffold(
      backgroundColor: const Color(0xFF0769AA),
      body: Center(
          child: Image.asset('assets/images/genvcarelogo.png'),

      ),
    );
  }
}

pubspec.yml:

assets:
    - images/genvcarelogo.png

【问题讨论】:

  • 确保您在 pubspec.yaml 中的缩进正确,否则图像将不会显示。还要确保您已将图像添加到 assets/images 文件夹中。

标签: flutter


【解决方案1】:

您不需要在 pubspec.yaml 中指定所有图像,只需添加包含所有图像的根文件夹,如下所示:

assets:
- assets/images/

并像你已经在使用它一样使用它:

Image.asset('assets/images/genvcarelogo.png')

【讨论】:

  • 不工作.......
  • 为什么要接受一个不起作用的答案?
【解决方案2】:

图像在pubspec.yaml 和代码中的位置不同,并且您的 pubspec 中的缩进错误。如果代码中的路径正确,则将您的 pubspec 更新为此,

assets:
  - assets/images/genvcarelogo.png

(确保第二行只有两个空格。)

【讨论】:

  • 无法正常工作我无法加载 genvcarelogo.png
  • 您是否在编辑 pubspec.yaml 后分离应用并再次运行?
【解决方案3】:

如果您确保 pubspec.yaml 中的所有内容都正确(即图像位置正确且缩进正确),那么您需要做的就是完全停止应用程序并重新运行它。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 2019-09-27
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多