公共规范Yaml

flutter:
  uses-material-design: true
  assets:
    - lib/images/
    - lib/images/app_logo.png

主.dart
@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          color: Colors.blueAccent,
          image: DecorationImage(
            image: ExactAssetImage('lib/images/app_logo.png'),
            fit: BoxFit.cover,
          ), //DecorationImage
        ), //BoxDecoration
      ),//Container
    ); //Scaffold
  }

应用程序中没有任何错误,只是不会显示任何内容。以前从未见过。已经在pubspec中尝试了各种不同的缩进。已尝试删除-和lib/images之间的空间,并替换为制表符。
什么给予?

最佳答案

即使在lib文件夹中,代码也能正常工作。让我分享我的代码
这是我的代码

name: nav
description: A new Flutter project.

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

  assets:
    - lib/images/
    - lib/images/app_logo.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(),
      body: Container(
        height: 200,
        decoration: BoxDecoration(
          color: Colors.blueAccent,
          image: DecorationImage(
            image: ExactAssetImage('lib/images/app_logo.jpg'),
            fit: BoxFit.cover,
          ), //DecorationImage
        ), //BoxDecoration
      ),//Container
    )); //Scaffold
  }
}

您可以尝试的解决方案:
尝试从头重新启动应用程序(而不是通过shift+r)
请确保您正确地给出了适当的间距和文件扩展名

关于image - flutter 不会显示 Assets 图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56890548/

10-09 04:21