嘗試在我的 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/
(縮進與我專案的實際檔案中使用的相同)
uj5u.com熱心網友回復:
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;
});
}
},
),
),
);
}
}
uj5u.com熱心網友回復:
只需從您分配給 Image 小部件的路徑中洗掉 /
Image.asset('assets/images/small.png'),
uj5u.com熱心網友回復:
試試這個。這會很有幫助:
Image(image: AssetImage(""));
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398568.html
