当我尝试在Flutter应用程序(在Android模拟器上运行)的appBar中添加资产图像时,终端中反复出现以下异常:
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: /assets/images/small.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "/assets/images/small.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#64048(), name:
"/assets/images/small.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
抛出了另一个异常:RenderFlex在右侧溢出了117个像素。
- 这是应用程序主目录的代码:**
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gofundleaf/screens/profile.dart';
import 'package:gofundleaf/services/auth_service.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool _loading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Container(
padding: const EdgeInsets.only(left: 3, right: 3),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Image.asset('/assets/images/small.png'),
const Text('leaf')
],
),
],
),
),
),
body: Center(
child: _loading
? const CupertinoActivityIndicator()
: ElevatedButton(
child: const Text('Login'),
onPressed: () async {
setState(() {
_loading = true;
});
final user = await AuthService.login();
if (user != null) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => Profile(user: user),
),
);
} else {
setState(() {
_loading = false;
});
}
},
),
),
);
}
}
pubspec. yaml文件的结构如下:
name: gofundleaf
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.15.1 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
google_sign_in: ^5.2.1
http: ^0.13.4
url_launcher: ^6.0.17
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
uses-material-design: true
assets:
- assets/images/
(The缩进与我的项目的实际文件中使用的缩进相同)
4条答案
按热度按时间fzwojiic1#
只需从要分配给图像小部件的路径中删除/,如
fv2wmkja2#
olmpazwi3#
删除assets/path前面的/斜杠,它应该为
Image.asset('assets/images/small.png')
此外,请仔细检查映像名称,然后保存文件并热重启。
new9mtju4#
确保在pubspec.yaml
的asset部分声明了图像
每个你想要能够载入的图像都应该被声明,如果一个丢失了flutter将无法载入它