【问题标题】:How to add Flutter Firebase VIdeo Upload Progrese Indecator | Upload Progress Indicator |如何添加 Flutter Firebase VIdeo Upload Progrese Indecator |上传进度指示器 |
【发布时间】:2021-09-10 21:11:48
【问题描述】:

如何添加 Flutter Firebase VIdeo Upload Progrese Indecator |上传进度指示器 | 任何人都可以帮助我

MediaInfo compressVideo = await VideoCompress.compressVideo(
  videoPath,
  quality: VideoQuality.Res640x480Quality,
  includeAudio: true,
);
print('video Compressing Done');

var uploadVideo = await FirebaseStorage.instance
    .ref()
    .child("videos/${Me.get().uid}/${videoDoc.id}")
    .putFile(File(compressVideo?.path));
print('video Uploading Done');

final videoUrl = await uploadVideo.ref.getDownloadURL();
showToast("Upload Successful");

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    这应该是一个很好的例子:

    Future<void> handleTaskExample2(String filePath) async {
      File largeFile = File(filePath);
    
      firebase_storage.UploadTask task = firebase_storage.FirebaseStorage.instance
          .ref("videos/${Me.get().uid}/${videoDoc.id}")
          .putFile(File(compressVideo?.path));
    
      task.snapshotEvents.listen((firebase_storage.TaskSnapshot snapshot) {
        print('Task state: ${snapshot.state}');
        print(
            'Progress: ${(snapshot.bytesTransferred / snapshot.totalBytes) * 100} %');
      }, onError: (e) {
        // The final snapshot is also available on the task via `.snapshot`,
        // this can include 2 additional states, `TaskState.error` & `TaskState.canceled`
        print(task.snapshot);
    
        if (e.code == 'permission-denied') {
          print('User does not have permission to upload to this reference.');
        }
      });
    
      // We can still optionally use the Future alongside the stream.
      try {
        await task;
        print('Upload complete.');
      } on firebase_core.FirebaseException catch (e) {
        if (e.code == 'permission-denied') {
          print('User does not have permission to upload to this reference.');
        }
        // ...
      }
    }
    

    我还建议查看docs 了解更多详情。

    【讨论】:

    • 我已经尝试过了。在我的情况下它不起作用
    【解决方案2】:

    终于,我得到了正确的答案。 这个答案在我的情况下非常有效。

    //This is the document reference 
    Reference videoRef = FirebaseStorage.instance
        .ref()
        .child("videos/${Me.get().uid}/${videoDoc.id}");
    
    //Here video is uploading and return UploadTask
    UploadTask videoTask = videoRef.putFile(compressVideo.file);
    
    //This is upload task StreamSubscription
    final StreamSubscription<void> streamSubscription =
        videoTask.snapshotEvents.listen((data) {
      double uploadPer;
      setState(() => uploadPer = (data.bytesTransferred / data.totalBytes)* 
     100);
    print(uploadPer); 
    });
    
    //When video uploading is completed then streamSubscription is cancel.
    await videoTask.whenComplete(() => null);
    streamSubscription.cancel();
    
    //This is finally get video download URL 
    final String videoUrl = await videoRef.getDownloadURL();
    print(videoUrl);
    showToast("Upload Successful");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2020-02-10
      • 2021-07-25
      • 2021-01-02
      • 2018-10-01
      • 2016-06-13
      • 2019-03-06
      相关资源
      最近更新 更多