dart 上传视频到Firebase->FireStorage时出错,[error]未处理的异常:用于空值的空校验运算符

z2acfund  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(104)

我试图从画廊上传视频,它给我的错误,但它的工作正常上传视频从相机在我的android 13物理设备.当我试图从我的Android 8模拟器上传视频时,它在相机和图库中都工作正常。
错误:-

我得到这个错误,当我尝试从画廊上传视频.
下面是用于拾取视频的方法的代码。

videoPicker(ImageSource src) async {
    final video = await ImagePicker().pickVideo(source: src);
    if (video != null) {
      Get.to(
          AddCaptionScreen(videoFile: File(video.path), videoPath: video.path));
    } else {
      Get.snackbar("Error Occurred",
          "Error Occurred while Selecting video. Please try again");
    }
  }

下面是上传videl的GetxController代码=>

uploadVideo(String songName, String caption, String videoPath) async {

    try{
      String uid = FirebaseAuth.instance.currentUser!.uid;

      DocumentSnapshot userDoc =
      await FirebaseFirestore.instance.collection("users").doc(uid).get();

      String videoID = uuid.v1();

      String videoUrl = await _uploadVideoToStorage(videoID, videoPath);  // THIRD LINE ERROR HERE

      String thumbnail = await _uploadVideoThumbtoStorage(videoID, videoPath);

      VideoModel videoModel =  VideoModel(username:  (userDoc.data()! as Map<String , dynamic>)['name'], uid: uid, thumbnail: thumbnail, caption: caption, commentsCount: 0, videoID: videoID, likes: [], profilePic: (userDoc.data()! as Map<String , dynamic>)['profilePic'], shareCount: 0, songName: songName, videoUrl: videoUrl);

      await FirebaseFirestore.instance.collection("videos").doc(videoID).set(videoModel.toJson());
      Get.snackbar("Video Uploaded Successfully", "Thank You Sharing Your Content");
      Get.back();
    }catch (e){
      print(e);
      rethrow;
    }

  }

  // below function used to Compress and upload video to firestorage
  Future<String> _uploadVideoToStorage(String videoId, String videoPath) async {
    print("VideoID  ------->   $videoId ,,   VideoPath -------> $videoPath");
    Reference reference =
        FirebaseStorage.instance.ref().child("videos").child(videoId);     // SECOND LINE ERROR HERE



    UploadTask uploadTask = reference.putFile( await _compressVideo(videoPath));
    TaskSnapshot snapshot = await uploadTask;
    String downloadUrl = await snapshot.ref.getDownloadURL();
    return downloadUrl;
  }

  _compressVideo(String videoPath) async {
    final compressedVideo = await VideoCompress.compressVideo(videoPath,
        quality: VideoQuality.MediumQuality);
    return compressedVideo!.file;                                          // FIRST LINE ERROR HERE
  }

  //below functions used for Generate Thumbnail and upload on firestorage
  Future<File> _generateThumbnail(String videoPath) async {
    final thumbnail = await VideoCompress.getFileThumbnail(videoPath);
    return thumbnail;
  }

  Future<String> _uploadVideoThumbtoStorage(String id, String videoPath) async {
    Reference reference =
        FirebaseStorage.instance.ref().child("thumbnail").child(id);
    UploadTask uploadTask =
        reference.putFile(await _generateThumbnail(videoPath));
    TaskSnapshot snapshot = await uploadTask;
    String downloadUrl = await snapshot.ref.getDownloadURL();
    return downloadUrl;
  }
}

我试图上传视频与良好的互联网速度。但给我同样的错误。我也在挑选后播放视频,它在android 8模拟器和android 13物理设备中播放正常。还试图在AndroidManifest.xml中添加权限,但没有任何改变,因为它选择视频和播放,但只是没有上传。

ffx8fchx

ffx8fchx1#

看起来您使用的方法可能会输出空值。
要解决此问题,请考虑以下步骤:
1.探索如何使用专为挑选视频而设计的软件包。这种方法对于处理各种场景更鲁棒。如果您选择创建自己的方法,请准备好在不同的设备和用例中进行彻底的测试。
1.在继续执行任何操作之前,请先检查您正在使用的值,以确保该值不为null。
1.避免将上传的文件格式仅限于MP4。支持更广泛的文件类型以获得更大的灵活性是有益的。
希望这些建议对大家有帮助!

相关问题