出現此錯誤“影像資源服務捕獲的例外”。 我已經在pubspec.yaml檔案中添加了資產,但仍然出現此錯誤,有人知道為什么嗎?所有檔案和由此產生的錯誤都被記錄下來。 專案中使用的影像是“https://photos.app.goo.gl/n5s5yiQnhYmFzVs1A”。
檔案 #1 main.dart
import 'package:flutter/material.dart';
import 'package:flutter_application_1/pages/welcome_page.dart';
void main(){
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Demo",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const WelcomePage(),
);
}
}
檔案 #2welcome_page.dart
import 'package:flutter/material.dart';
class WelcomePage extends StatefulWidget {
const WelcomePage({ Key? key }) : super(key: key);
@override
State<WelcomePage> createState() => _WelcomePageState();
}
class _WelcomePageState extends State<WelcomePage> {
List images = [
"welcome-one.png",
"welcome-two.png",
"welcome-three.png",
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView.builder(
scrollDirection: Axis.vertical,
itemCount: images.length,
itemBuilder: (_,index){
return Container(
width: double.maxFinite,
height: double.maxFinite,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
"img/" images[index]
))
),
);
}),
);
}
}
pubspec.yaml 檔案
flutter:
uses-material-design: true
assets:
- img\welcome-one.png
- img\welcome-two.png
- img\welcome-three.png
錯誤資訊
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: img/welcome-one.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "img/welcome-one.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#acc1c(), name: "img/welcome-one.png", scale: 1.0)
uj5u.com熱心網友回復:
假設您在專案的根目錄(與lib
檔案夾相同級別)中有一個名為的檔案夾img
,那么我看到的唯一問題是您需要使用正斜杠,而不是反斜杠pubspec.yaml
// changing \ to /
assets:
- img/welcome-one.png
- img/welcome-two.png
- img/welcome-three.png
您也不需要指定每個影像,您可以將其縮短為此
assets:
- img/
這將針對img
檔案夾中的所有內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/470224.html
上一篇:來自TypeScript的匿名型別物件的Dart等效項
下一篇:顫振影像縮放但如何更改縮放位置