图像未在flutter android中加载
Posted
技术标签:
【中文标题】图像未在flutter android中加载【英文标题】:Image Not Loading in flutter android 【发布时间】:2021-05-16 19:06:56 【问题描述】:我尝试了所有方法,但似乎没有任何效果。 据我所知,图像的路径是正确的。 (虽然我附上了一张图片供参考)。这是我得到的错误 -
════════ 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*-
名称:foodfast 描述:一个新的 Flutter 项目。
publish_to: '无'
版本:1.0.0+1
环境: sdk: ">=2.7.0
依赖: 扑: sdk:颤动
启动画面:^1.3.5
cupertino_icons: ^1.0.0 firebase_auth: ^0.20.0+1 cloud_firestore: ^0.16.0
dev_dependencies: 颤振测试: sdk:颤动
颤动:
使用材料设计:是的
资产: - 资产/start.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')
],
)),
);
【问题讨论】:
【参考方案1】:您需要先添加图片路径,即assets,然后添加图片名称
Image(image: AssetImage('assets/start.png'), fit: BoxFit.cover),
【讨论】:
不是那么直观。谢谢。【参考方案2】:提供完整路径,即使您已经在 pubspec.yaml 中提供了它。
因此,应该是'assets/start.png'
【讨论】:
【参考方案3】:在您的图片路径中使用像 'assets/start.png' 这样的完整路径。
Image(image: AssetImage('assets/start.png'), fit: BoxFit.cover),
【讨论】:
【参考方案4】:尝试更改路径
AssetImage("/image/start.png")
到
AssetImage("./image/start.png")
(.
表示转到最高文件夹级别)
这对我有用!
【讨论】:
以上是关于图像未在flutter android中加载的主要内容,如果未能解决你的问题,请参考以下文章