使用onComplete函数将图像添加到Firebase存储的方法是什么 [英] What is the the method to use onComplete function to add Images to Firebase Storage

查看:23
本文介绍了使用onComplete函数将图像添加到Firebase存储的方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了将三个图像上传到Firebase存储的功能.我一直在教程的帮助下做到这一点.由于本教程使用的是旧版本代码,因此我尝试将其最大值更改为新版本.在本教程中,它使用 onComplete 方法获取任务快照.但是在pub.dev文档中,据说这些方法已被删除,它们是一个异常,像这样( BREAKING:现已删除isCanceled,isComplete,isInProgress,isPaused和isSuccessful.相反,您应该订阅流(用于暂停/进行中/完成/错误事件)或任务未来"以完成任务/错误. )

I have made a function to upload three images to firebase storage. I have been doing it with the help of a tutorial. Since the tutorial uses old version codes I am trying my maximum to change it to a newer version. In the tutorial, it uses onComplete method to get the task snapshot. But in the pub.dev docs it is said that these methods have been removed and their is an exception Like this( BREAKING: isCanceled, isComplete, isInProgress, isPaused and isSuccessful have now been removed. Instead, you should subscribe to the stream (for paused/progress/complete/error events) or the task Future for task completion/errors.)

请说明这是什么意思,以及如何根据它更改下面的代码.在我输入 onComplete

please tell what does this mean and how can I change my below code according to it. there is an error showing in the place where I type onComplete

void validateAndUpload() async{
    if (_formKey.currentState.validate()) {
      if (_image1 != null && _image2 != null && _image3 != null) {
        if (selectedSizes.isNotEmpty) {
          String imageUrl1;
          String imageUrl2;
          String imageUrl3;
          final FirebaseStorage storage = FirebaseStorage.instance;
          final String picture1 =
              '1${DateTime.now().millisecondsSinceEpoch.toString()}.jpg';
          UploadTask task1 = storage.ref().child(picture1).putFile(_image1);

          final String picture2 =
              '2${DateTime.now().millisecondsSinceEpoch.toString()}.jpg';
          UploadTask task2 = storage.ref().child(picture2).putFile(_image2);

          final String picture3 =
              '3${DateTime.now().millisecondsSinceEpoch.toString()}.jpg';
          UploadTask task3 = storage.ref().child(picture3).putFile(_image3);

        TaskSnapshot snapshot1 = await task1.onComplete.then((snapshot) => snapshot);
        TaskSnapshot snapshot2 = await task1.onComplete.then((snapshot) => snapshot);

        task3.onComplete.then((snapshot3) async{
             imageUrl1 = await snapshot1.ref.getDownloadURL();
             imageUrl2 = await snapshot2.ref.getDownloadURL();
             imageUrl3 = await snapshot3.ref.getDownloadURL();
        });


        } else {
          Fluttertoast.showToast(
              msg: "Sizes cannot be Empty",
              backgroundColor: Colors.red,
              textColor: Colors.white);
        }
      } else {
        Fluttertoast.showToast(
            msg: "Images are not Filled",
            backgroundColor: Colors.red,
            textColor: Colors.white);
      }
    }
  }

推荐答案

您快到了!如错误所示,您要么必须监听 UploadTask 流,要么等待 UploadTask 完成.对于您的情况,

You are almost there! As the error says either you have to listen for the UploadTask streams or await for UploadTask to complete. For your case,

TaskSnapshot snapshot1 = await task1;
TaskSnapshot snapshot2 = await task2;
TaskSnapshot snapshot3 = await task3;

imageUrl1 = await snapshot1.ref.getDownloadURL();
imageUrl2 = await snapshot2.ref.getDownloadURL();
imageUrl3 = await snapshot3.ref.getDownloadURL();

更多参考- https://firebase.flutter.dev/docs/storage/usage/#handling-tasks

这篇关于使用onComplete函数将图像添加到Firebase存储的方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆