首页 > 解决方案 > 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]])
  });
},
),
  );

  }

标签: flutterentity-frameworkdartflutter-animation

解决方案


您可以检查数组长度并将其减一。


推荐阅读