我尝试了所有方法,但似乎没有任何效果。 据我所知,图像的路径是正确的。 (虽然我附上了一张图片供参考)。这是我得到的错误 -
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: start.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668
#2 AssetBundleImageProvider.load
package:flutter/…/painting/image_provider.dart:651
#3 ImageProvider.resolveStreamForKey.<anonymous closure>
package:flutter/…/painting/image_provider.dart:504
...
Image provider: AssetImage(bundle: null, name: "start.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#2bd13(), name: "start.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════
这是**pubspec.yaml*-
名称:快餐 描述:一个新的 Flutter 项目。
publish_to: '无'
版本:1.0.0+1
环境: sdk:">=2.7.0 <3.0.0"
依赖: 扑: sdk:颤动
启动画面:^1.3.5
cupertino_icons:^1.0.0 firebase_auth:^0.20.0+1 cloud_firestore:^0.16.0
dev_dependencies: 颤振测试: sdk:颤动
颤动:
使用材料设计:真实
资产: - 资产/开始.png
sign_in.dart
import 'package:flutter/material.dart';
class SignIn extends StatefulWidget {
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('sign in'),
),
backgroundColor: Colors.white,
body: Center(
child: ListView(
children: <Widget>[
Image(image: AssetImage('start.png'), fit: BoxFit.cover),
new Text('text below image')
],
)),
);
}
}
答案 0 :(得分:3)
您需要先添加图片路径,即assets,然后添加图片名称
Image(image: AssetImage('assets/start.png'), fit: BoxFit.cover),
答案 1 :(得分:1)
即使您已经在 pubspec.yaml 中提供了完整路径,也要提供完整路径。
因此,它应该是 'assets/start.png'
答案 2 :(得分:1)
在图像路径中使用完整路径,例如 'assets/start.png'。
Image(image: AssetImage('assets/start.png'), fit: BoxFit.cover),