繁体   English   中英

"RangeError(索引):无效值:不在包含范围内 0..1:2 并且应用程序在此之后崩溃"

[英]RangeError (index): Invalid value: Not in inclusive range 0..1: 2 and app crashing after that

我看到了其他答案,他们通过在列表视图中添加 itemcount 得到了解决,但我仍然收到此错误。 对于添加图像的相同代码,我没有收到此错误。 在此我使用图像选择器将视频上传到列表并在列表视图中呈现,然后上传到 firebase

添加视频屏幕:

class _addVideoState extends State<addVideo> {
  bool uploading = false;
  double val = 0;
  CollectionReference? vidRef;
  firebase_storage.Reference? ref;
  File? video;
  VideoPlayerController? videoPlayerController;

  final picker = ImagePicker();

  Future pickVideo(ImageSource source) async {
    final Video = await picker.pickVideo(source: source);

    video = File(Video!.path);

    videoPlayerController = VideoPlayerController.file(video!)
      ..initialize().then((_) {
        setState(() {});
        videoPlayerController!.play();
      });
  }

  List<File> _video = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Add video'),
        actions: [
          ElevatedButton(
            onPressed: () {
              setState(() {
                uploading = true;
              });

              uploadFile().whenComplete(() => Navigator.of(context).pop());
            },
            child: Text(
              'Upload Video',
              style: TextStyle(color: Colors.red),
            ),
          )
        ],
      ),
      body: ListView.builder(
          itemCount: _video.length + 1,
          itemBuilder: (ctx, index) {
            return index == 0
                ? Center(
                    child: ElevatedButton(
                        onPressed: () {
                          !uploading ? chooseVideo() : null;
                        },
                        child: const Text(
                          'Test',
                          style: TextStyle(color: Colors.black),
                        )),
                  )
                : AspectRatio(
                    aspectRatio: videoPlayerController!.value.aspectRatio,

                    // child: VideoPlayer(videoPlayerController!),
                    child: VideoPlayer(VideoPlayerController.file(_video[index])
                      ..initialize().then((_) {
                        setState(() {});
                        videoPlayerController!.play();
                      })),
                  );
          }),
    );
  }

  chooseVideo() async {
    ImagePicker picker = ImagePicker();

    final pickedFile = await picker.pickVideo(source: ImageSource.gallery);

    setState(() {
      _video.add(File(pickedFile!.path));
    });
    videoPlayerController = VideoPlayerController.file(File(pickedFile!.path))
      ..initialize().then((_) {
        setState(() {});
        videoPlayerController!.play();
      });
    if (pickedFile.path == null) retrieveLostData();
  }

您正在显示索引 == 0 的 ElevatedButton。因此您访问 _video 的索引从 1 开始,因此您必须减少 1 个索引以保持在正确的范围内。

尝试替换这一行:

child: VideoPlayer(VideoPlayerController.file(_video[index - 1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM