Click here to Skip to main content
15,346,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to upload videos to firebase before that I am trying to display them in the app in listview but they are not getting displayed on the screen, there is'nt any error messages in debug console either so I am unable to figure out what is the issue; My upload video button which uploads videos to firebase is also not functioning so I think maybe videos are not adding to the list Add_video_screen class :

class addVideo extends StatefulWidget {
  const addVideo({Key? key}) : super(key: key);

  @override
  _addVideoState createState() => _addVideoState();
}

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 - 1])
                          ..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();
  }

  Future<void> retrieveLostData() async {
    final LostDataResponse = await ImagePicker.platform.getLostData();
    if (LostDataResponse.isEmpty) {
      return;
    }
    if (LostDataResponse != null) {
      setState(() {
        _video.add(File(LostDataResponse.file!.path));
      });
    } else {
      print(LostDataResponse.file);
    }
  }

  Future uploadFile() async {
    int i = 1;

    for (var vid in _video) {
      setState(() {
        val = i / _video.length;
      });
     
      ref = firebase_storage.FirebaseStorage.instance
          .ref()
          .child('videos/${Path.basename(vid.path)}');
      await ref!.putFile(vid).whenComplete(() async {
       
        await ref!.getDownloadURL().then((value) {
          vidRef!.add({
            'url': value
          }); 
          i++;
        });
      });
    }
  }

  @override
  void initState() {
    super.initState();
    vidRef = FirebaseFirestore.instance.collection(
        'Videourls'); 
  }
}  


What I have tried:

I tried the same way for adding image and it is working perfectly
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900