图片资源服务捕获异常(已在pubspec.yaml文件中添加资源)
Exception caught by image resource service(already added assets in pubspec.yaml file)
出现此错误“图像资源服务捕获异常”。
我已经在 pubspec.yaml 文件中添加了资产,但仍然收到此 error 有人知道为什么吗?
所有文件和由此产生的错误都被记录下来。
项目中使用的图片为“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(),
);
}
}
文件 #2 welcome_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)
假设您的项目根目录中有一个名为 img
的文件夹(与 lib
文件夹同级),那么我看到的唯一问题是您需要使用正斜杠,而不是pubspec.yaml
中的反斜杠
// changing \ to /
assets:
- img/welcome-one.png
- img/welcome-two.png
- img/welcome-three.png
您也不需要指定每张图片,您可以将其缩短为
assets:
- img/
这将针对 img
文件夹中的所有内容。
出现此错误“图像资源服务捕获异常”。 我已经在 pubspec.yaml 文件中添加了资产,但仍然收到此 error 有人知道为什么吗? 所有文件和由此产生的错误都被记录下来。 项目中使用的图片为“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(),
);
}
}
文件 #2 welcome_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)
假设您的项目根目录中有一个名为 img
的文件夹(与 lib
文件夹同级),那么我看到的唯一问题是您需要使用正斜杠,而不是pubspec.yaml
// changing \ to /
assets:
- img/welcome-one.png
- img/welcome-two.png
- img/welcome-three.png
您也不需要指定每张图片,您可以将其缩短为
assets:
- img/
这将针对 img
文件夹中的所有内容。