请查找以下代码main. dart:
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar( title: Text(this.title), ),
body: Center( child: Image.asset("assets/smiley.png")),
);
}
}
- 出版物规范yaml**
name: product_app
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:
- assets/appimages/floppy.png
- assets/appimages/iphone.png
- assets/appimages/laptop.png
- assets/appimages/pendrive.png
- assets/appimages/pixel.png
- assets/appimages/tablet.png
执行前;资产assets/appimage/test.png不存在pubspec. yaml中的警告执行以下代码后出现异常
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞══
I/flutter ( 8653): The following assertion was thrown while resolving an image:
I/flutter ( 8653): Unable to load asset: AssetManifest.json
I/flutter ( 8653): When the exception was thrown, this was the stack:
I/flutter ( 8653): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 8653): <asynchronous suspension>
I/flutter ( 8653): #1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:67:33)
I/flutter ( 8653): #2 CachingAssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:163:18)
I/flutter ( 8653): #3 CachingAssetBundle.loadStructuredData (package:flutter/src/services/asset_bundle.dart:184:5)
I/flutter ( 8653): #4 AssetImage.obtainKey (package:flutter/src/painting/image_resolution.dart:174:18)
I/flutter ( 8653): #5 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:318:15)
I/flutter ( 8653): #9 ImageProvider.resolve (package:flutter/src/painting/image_provider.dart:315:16)
I/flutter ( 8653): #10 _ImageState._resolveImage (package:flutter/src/widgets/image.dart:1010:20)
I/flutter ( 8653): #11 _ImageState.didChangeDependencies (package:flutter/src/widgets/image.dart:967:5)
I/flutter ( 8653): #12 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4376:12)
I/flutter ( 8653): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter ( 8653): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
5条答案
按热度按时间gcmastyq1#
我的朋友..把你的assets文件夹从lib文件夹中移出来-〉它是一个基于项目的文件夹..在那之后,你的代码就可以正常工作了。另外,在
pubspec.yaml
中,就像Aamil Silawat
说的那样,把它变成assets/appimages/
。lstz6jyr2#
pubspec.yaml
文件中简单地定义,如下所示。无需在.yaml file
中定义所有图像****
cl25kdpy3#
这对我很有效
pubspec.yaml
main.dart
jjhzyzn04#
请参见有时它显示**{您的图像文件夹}在您的lib文件夹之外(我的意思是它不在任何文件夹中)在文件资源管理器中,但如果您仔细查看(在编辑器资源管理器中)该文件夹位于lib或任何其他文件夹中,它ID不是空闲的(在我的情况下发生了这种情况)**
9nvpjoqh5#
直接保存到lib中,然后写“lib/imagename”。扩展名对我很有效。
如果这不起作用,然后改变你的渠道测试版,再试一次。