0

我有这个将视频上传到firebase的未来功能,我想以百分比跟踪这个上传过程,所以在上传过程完成后,我会得到网址。

代码

Future storageupload() async {
    try {
      if (controller = null) {
        dialog('Error', 'Please Provide A Video Name', () => {});
      } else {
        StorageReference ref = FirebaseStorage.instance
            .ref()
            .child("Khatma 1")
            .child("Videos")
            .child(controller.text != null ? controller.text : "");
        StorageUploadTask uploadTask = ref.putFile(
            File(Variables.lastVideoPath),
            StorageMetadata(contentType: 'video/mp4'));
      }
    } catch (e) {
      print(e);
    }
  }

  Future uploadToStorage() async {
    try {
      await storageupload();
      final downloadUrl = await FirebaseStorage.instance
          .ref()
          .child("Khatma 1")
          .child('Videos')
          .child(controller.text)
          .getDownloadURL();

      final String url = downloadUrl.toString();

      print(url);
    } catch (error) {
      print(error);
    }
  }
4

1 回答 1

2

您可以通过收听TaskSnapshotStream 获得。

uploadTask.snapshotEvents.listen((TaskSnapshot snapshot) {
      double _progress = snapshot.bytesTransferred.toDouble() / snapshot.totalBytes.toDouble();
    });

欲了解更多信息:https ://pub.dev/documentation/firebase_storage/latest/firebase_storage/StorageTaskSnapshot-class.html

如果您使用的是旧版本的 Firebase 存储,则 iesnapshotEvents不可用。

这应该适合你。

uploadTask.events.listen((event) {
  double _progess = event.snapshot.bytesTransferred.toDouble()  / event.snapshot.totalByteCount.toDouble();
}); 
于 2020-10-06T06:54:52.213 回答