RangeError (index): Invalid value: Valid value range is empty: 0, get this Error on delete last image flutter:-

时间:2021-05-14 04:36:32

标签: flutter entity-framework dart flutter-animation

我有五张图片,当我点击任何图片时,它会全屏打开,我们可以向左/向右滑动到其他图片,当我删除任何图片时,特定图片会被删除,然后转到下一张图片,到目前为止它工作正常但是当删除所有其他图像后只剩下图像并且当我删除单个图像时它也会给出错误: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]])
  });
},
),
  );

  }

1 个答案:

答案 0 :(得分:2)

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