[英]How to resolve it “RangeError (index): Invalid value : Valid value range is empty : 0” in flutter?
I'm to trying to get images from firestore database and put onto String type list and want to show as the slider Image.我试图从 firestore 数据库中获取图像并将其放入字符串类型列表中,并希望显示为 slider 图像。 but the above error is occurring on Screen but after milliseconds, error goes and images show as slider但上述错误发生在屏幕上,但毫秒后,错误消失,图像显示为 slider
Decleared Global Varible:清除全局变量:
List<String> getSliderImages=[];
Method for fetched images from firestore:从 Firestore 获取图像的方法:
And this method is calling inside initState()
method这个方法在initState()
方法内部调用
void getSliderImage(){
List<String> userId=[];
Firestore.instance.collection("Slider").getDocuments()
.then((QuerySnapshot snapshot) {
snapshot.documents.forEach((f){
setState(() {
userId.add(f.documentID);
});
});
for(int i=0;i<userId.length;i++){
setState(() {
print('snap.documentID_IF_userId :${userId[i]}');
Firestore.instance.collection('Slider').document(userId[i]).get().then((DocumentSnapshot document){
String image=document['Image'];
getSliderImages.add(image);
print('snap.documentID_IF_userId_IMAGE :$image');
print("getSliderImages:$getSliderImages");
});
});
}
}).catchError((onError){
print(onError);
setState(() {
Fluttertoast.showToast(msg: "$onError");
});
});
}
Slider widget, there I want to display images: Slider 小部件,我想在那里显示图像:
Here I used carousel_pro plugin for Slider这里我使用了 Slider 的carousel_pro插件
Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(30.0)),
//color: Colors.black
),
height: MediaQuery.of(context).size.height/5,
width: MediaQuery.of(context).size.height/2,
child: Carousel(
images: [
new NetworkImage(getSliderImages[0]),
new NetworkImage(getSliderImages[1]),
new NetworkImage(getSliderImages[2]),
new NetworkImage(getSliderImages[3]),
],//getSliderUserId
showIndicator: true,
borderRadius: true,
moveIndicatorFromBottom: 100.0,
noRadiusForIndicator: false,
overlayShadow: false,
overlayShadowColors: Colors.white,
overlayShadowSize: 0.7,
)
),
I have solved it by myself, this error was occurring because of getSliderUserId[]
was empty at initially.我自己解决了这个错误,因为getSliderUserId[]
最初是空的。 so, I put a condition on the widget.所以,我在小部件上设置了一个条件。 if getSliderImages.isEmpty
then show progress indicator otherwise Display Slider Image.如果getSliderImages.isEmpty
则显示进度指示器,否则显示 Slider 图像。
Now it's working perfectly without getting indexing error:)现在它可以正常工作而不会出现索引错误:)
Here Modified code:这里修改代码:
Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(30.0)),
//color: Colors.black
),
height: MediaQuery.of(context).size.height/5,
width: MediaQuery.of(context).size.height/2,
child: getSliderImages.isEmpty?
CircularProgressIndicator()
:
Carousel(
images: [
new NetworkImage(getSliderImages[0]),
new NetworkImage(getSliderImages[1]),
new NetworkImage(getSliderImages[2]),
new NetworkImage(getSliderImages[3]),
],
showIndicator: true,
borderRadius: true,
moveIndicatorFromBottom: 100.0,
noRadiusForIndicator: false,
overlayShadow: false,
overlayShadowColors: Colors.transparent,
overlayShadowSize: 0.7,
)
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.