dart - 根据官方文档,Flutter 不显示图像

标签 dart flutter flutter-appbar

我正在学习一个流行的 flutter 教程,我似乎是唯一遇到这个问题的人,这也让导师感到困惑。

问题:按照官方文档,图片不显示。

变通解决方案:向引用该文件的小部件添加尾随“./”。

问题:为什么会这样?

pubspec.yaml 代码:

flutter:
  uses-material-design: true

  assets:
    - assets/food.jpg

没有尾随 './' 的代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('EasyList'),
        ),
        body: Card(child: Column(children: <Widget>[
          Image.asset('assets/food.jpg'),
          Text('Food Paradise')
        ],),),
      ),
    );
  }
}

尾随 './' 的代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('EasyList'),
        ),
        body: Card(child: Column(children: <Widget>[
          Image.asset('./assets/food.jpg'),
          Text('Food Paradise')
        ],),),
      ),
    );
  }
}

最佳答案

你看我刚刚用你上面指出的代码创建了一个项目,它在没有 ``/` 的情况下也能正常工作,然后我分享你使用的代码和项目的结构。

文件 main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('EasyList'),
        ),
        body: Card(
          child: Column(
            children: <Widget>[
              Image.asset('assets/food.jpg'),
              Text('Food Paradise')
            ],
          ),
        ),
      ),
    );
  }
}

文件 pubspec.yaml 名称:普鲁巴 描述:一个新的 Flutter 项目。

# 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.
# Read more about versioning at semver.org.
version: 1.0.0+1

environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # 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
  assets:
    - assets/food.jpg

这是项目的结构

enter image description here

结果如下:

enter image description here

您分享的代码看起来不错,如果一切配置良好,它应该可以正常工作。

关于dart - 根据官方文档,Flutter 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54170320/

相关文章:

flutter 网络 : Is it Firebase Analytics support in Flutter web application?

android - 是否可以在 Flutter 中使用手写笔输入?

flutter - 在 flutter 中制作自定义弯曲应用栏的更好方法

flutter - 全局编辑 AppBar 操作列表中 TextButton 小部件的颜色?

dart - Polymer.dart 中生命周期事件的顺序

flutter - 如何从 Flutter Web 中的文本小部件中选择和复制文本?

flutter - 导航时未定义的 'context'

系统状态下的Flutter容器

android - 如何使用 Dart 签署 Binance HTTP 请求

objective-c - 如何在 Dart 中记录当前函数名称?