Flutter未根据官方文档显示图像

时间:2019-01-13 15:26:49

标签: dart flutter flutter-appbar

我正在关注一个流行的flutter教程,而且我似乎是唯一遇到此问题的人,这也使教师感到困惑。

问题: 图片未根据官方文档显示。

解决方法: 在引用文件的小部件中添加尾随“ ./”。

问题: 为什么会这样?

pubspec.yaml代码:

<html>

<head>
  <title></title>
  <link rel="stylesheet" href="styles/index.css">
</head>

<body>
  <header>
    <h3 id="banner">samplewebsite.com</h3>
    <p id="account">Sign In</p>
  </header>
</body>

</html>

代码,不带'./'

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')
        ],),),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

看起来我刚刚使用上面指定的代码创建了一个项目,并且在没有``/`的情况下可以正常工作,然后我共享了您使用的代码和项目的结构。

文件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     名称:prueba     描述:一个新的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

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