flutter - RangeError(索引):无效值:有效值范围为空:0,删除最后一个图像抖动时得到此错误:-
问题描述
我有五个图像,当我点击任何图像时,它会全屏打开,我们可以向左/向右滑动到其他图像,当我删除任何图像时,特定图像会被删除,然后它会转到下一个图像,直到现在工作正常,但是当删除所有其他图像后只剩下图像时,当我也删除一个图像时,它会给出错误:RangeError(索引):无效值:有效值范围为空:0。如何删除最后一张图像index.Pls 帮助...谢谢。
Widget deleteBtn(BuildContext context) {
final auth = FirebaseAuth.instance;
return new Padding(
padding: const EdgeInsets.all(10.0),
child: IconButton(
icon: Icon(
Icons.delete_forever_outlined,
color: const Color(0xFF227770),
size: 35,
),
onPressed: () async {
var fileUrl =
Uri.decodeFull(Path.basename(widget.imageList[_currentIndex]))
.replaceAll(new RegExp(r'(\?alt).*'), '');
try {
firebase_storage.Reference photoRef = await firebase_storage.FirebaseStorage.instance
.ref().storage.refFromURL(widget.imageList[_currentIndex]);
await photoRef.delete().then((value) {
widget.imageList.removeAt(_currentIndex);
_pageController.nextPage(duration: Duration(milliseconds: 10) , curve: Curves.easeInOut);
});
} catch (e) {
print(e);
print("Cant delete" +e);
}
FirebaseFirestore.instance
.collection("users")
.doc(auth.currentUser.uid)
.update({
"images": FieldValue.arrayRemove([widget.imageList[_currentIndex]])
});
},
),
);
}
解决方案
您可以检查数组长度并将其减一。
推荐阅读
- jquery - 下拉雪佛龙不旋转
- firebase - 如何在 BigQuery 中合并 Google Ads 和 Firebase 数据
- android - 无法在华为和三星手机上更改可绘制颜色(色调)
- d3.js - tickSizeOuter 不等于零,但仅 yAxis 的第一个和最后一个刻度显示行
- java - Java 中的急切加载是什么?
- asp.net - EPPlus - 无法从 excel 中获取 NPV 和 IRR 函数的值
- iis - IIS 服务器是不是反映了变化?
- azure-data-factory - AzureDataFactory 如何存储过滤器操作的输出
- java - 如何通过 Springboot 服务的 2 个实例从共享位置并行访问文件?
- akka-http - akka-http中的拒绝和异常有什么区别