0

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

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

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

问:为什么会这样?

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

1 回答 1

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

这是项目的结构

在此处输入图像描述

结果如下:

在此处输入图像描述

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

于 2019-01-13T15:57:49.680 回答