请找到下面的代码主飞镖:
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")),
);
}
}
Pubspec.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
在执行之前;资产资产/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)
我的朋友..将您的资产文件夹移出lib文件夹->它是一个基于项目的文件夹..之后,您的代码将正常工作。 此外,在pubspec.yaml
中,正如Aamil Silawat
所说,让它assets/appimages/
。
您可以简单地在pubspec.yaml
文件中定义,如下所示 不需要定义.yaml file
中的所有图像
assets:
- assets/appimages/ (it will include all images in your appimages folder)
**
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/appimages/iphone.png")),
);
}
}
这对我有用
Pubspec.yaml
assets:
- lib/assets/images/
主飞镖
Image.asset(
'lib/assets/images/gato.jpg',
fit: BoxFit.cover
),
请参阅 有时它显示{您的图像文件夹}在您的 lib 文件夹之外(我的意思是它不在任何文件夹中(在文件资源管理器中,但如果您仔细查看(在编辑器资源管理器中(该文件夹驻留在 lib 或任何其他文件夹中,它 id 不自由((在我的情况下发生了这种情况((
直接保存在库中,然后写下"lib/imagename".extension 为我工作。
如果这不起作用,请将您的频道更改为测试版。 ,然后重试。