Flutter 本地图片加载不出来

pubspec.yaml 中添加

项目根目录/assets/images/ 中是存在该图片的
但图片无论怎么刷新都加载不出来,根据文档反复对了很多次,实在找到不到代码在哪里出了问题。

重点来了…重新启动后,图片出来了……

页面布局
以下为关键代码:

  assets:
    - assets/images/

Layout.dart

import 'package:flutter/material.dart';

import 'section/footer.dart';
import 'section/title.dart';
import 'section/reviews.dart';

class Layout extends StatefulWidget {
  @override
  _LayoutState createState() => _LayoutState();
}

class _LayoutState extends State<Layout> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
       	// 图片无法加载,
          Image.asset(
              'assets/images/avatar.png',
            width: 200,
            height: 200,
            fit: BoxFit.cover
          ),
          Reviews(),
          titleSection,
          textSection,
          footerSection(context),
        ],
      )
    );
  }

  Widget headImage() {

  }

  Widget textSection = Container(
    padding: const EdgeInsets.all(32),
    child: Text(
      'Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese '
          'Alps. Situated 1,578 meters above sea level, it is one of the '
          'larger Alpine Lakes. A gondola ride from Kandersteg, followed by a '
          'half-hour walk through pastures and pine forest, leads you to the '
          'lake, which warms to 20 degrees Celsius in the summer. Activities '
          'enjoyed here include rowing, and riding the summer toboggan run.',
      softWrap: true,
    ),
  );
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flutter加载本地图片需要使用 `AssetImage` 或 `ExactAssetImage` 类型的 ImageProvider。在使用时,需要将本地图片文件路径传递给 ImageProvider,其中文件路径可以是相对路径或绝对路径,但必须是相对于当前 Flutter 应用程序的根目录。 下面是一个简单的示例代码,演示了如何在 Flutter加载本地图片: ```dart import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Local Image Demo', home: Scaffold( appBar: AppBar( title: Text('Flutter Local Image Demo'), ), body: Center( child: Image( image: AssetImage('assets/images/flutter_logo.png'), ), ), ), ); } } ``` 在上面的示例代码中,我们使用 `AssetImage` 类型的 ImageProvider 来加载本地图片,并将图片路径设置为 `assets/images/flutter_logo.png`。需要注意的是,我们需要在 Flutter 应用程序的根目录下创建一个名为 `assets` 的文件夹,并在其中创建一个名为 `images` 的子文件夹,然后将要加载图片文件放到该子文件夹中。 在运行该示例代码之前,还需要在 Flutter 应用程序的 `pubspec.yaml` 文件中添加以下配置,以便告诉 Flutter 应用程序要加载哪些本地图片: ```yaml flutter: assets: - assets/images/ ``` 这样,我们就可以在 Flutter 应用程序中加载本地图片了。需要注意的是,Flutter 应用程序的根目录可以使用 `Directory.current` 来获取,具体实现可以参考 `path`、`dart:io` 等相关库的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值