你好,我正尝试在我的应用程序上制作一个旋转木马,但图像没有显示。代码和错误如下,如果有人能帮助我解决这个问题,我将不胜感激。
我的终端上的错误:
图像资源服务捕获的异常解决图像编解码器时引发了以下_Exception:异常:无效图像数据
代码:
class CarouselBuild extends StatefulWidget {
CarouselBuild({Key? key}) : super(key: key);
@override
State<CarouselBuild> createState() => _CarouselBuildState();
}
class _CarouselBuildState extends State<CarouselBuild> {
final urlImages = [
'https://unsplash.com/photos/VURwPtZqyF4',
'https://unsplash.com/photos/oCZHIa1D4EU',
'https://unsplash.com/photos/5-GNa303REg',
];
@override
Widget build(BuildContext context) {
return CarouselSlider.builder(
options: CarouselOptions(
enlargeCenterPage: true,
autoPlay: true,
aspectRatio: 2.0,
),
itemCount: urlImages.length,
itemBuilder: (context, index, realIndex) {
final urlImage = urlImages[index];
return buildImage(urlImage, index);
},
);
}
Widget buildImage(String urlImage, int index) => Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.3,
margin: EdgeInsets.symmetric(horizontal: 0),
color: Colors.grey,
child: Image.network(
urlImage,
fit: BoxFit.cover,
),
);
}
1条答案
按热度按时间f0brbegy1#
问题不在于代码,而在于图像路径。
您已将图像路径定义为:
然而,这些并不是实际的图片,而只是Unsplash页面。您提供的照片的现有路径将是: