Flutter & Firebase : How to check if my picture is uploaded to Firebase Storage Successfully?

拈花ヽ惹草 提交于 2021-02-10 20:04:27

问题


Flutter & Firebase : How to check if my picture is uploaded to Firebase Storage Successfully?

I am trying to find a way to see if the picture was upload successful so that I can start and stop my LoadingScreen().

final StorageReference firebaseStorageRef = FirebaseStorage.instance
        .ref()
        .child('$path/${userID}_${timeStamp}_${number}_${tag}.jpg');

final StorageUploadTask uploadTask = firebaseStorageRef.putFile(image);

I am looking forward to hearing from all of you. Thank you in advance!


回答1:


Here is how you can upload and verify if images are uploaded to FirebaseStorage:

  final StorageReference firebaseStorageRef = FirebaseStorage.instance
      .ref()
      .child('$path/${userID}_${timeStamp}_${number}_${tag}.jpg'); 
  StorageUploadTask uploadTask =
      firebaseStorageRef.putFile(image);
  StorageTaskSnapshot storageSnapshot = await uploadTask.onComplete;
  var downloadUrl = await storageSnapshot.ref.getDownloadURL();
  if (uploadTask.isComplete) {
    final String url = downloadUrl.toString();
    print(url);
    //Success! Upload is complete
  } else {
    //Error uploading file
  }

You will get the downloadUrl after the upload is complete.




回答2:


You can use the onComplete property of the StorageUploadTask.

  final StorageReference firebaseStorageRef = FirebaseStorage.instance
      .ref()
      .child('$path/${userID}_${timeStamp}_${number}_${tag}.jpg'); 
  StorageUploadTask uploadTask =
      firebaseStorageRef.putFile(image);
  StorageTaskSnapshot storageSnapshot = await uploadTask.onComplete;
  // Here you know that the picture/file is succesfully uploaded.
  // You can stop your LoadingScreen().

Note that you may use some try-catch clauses to handle the errors of the async method.



来源:https://stackoverflow.com/questions/63842732/flutter-firebase-how-to-check-if-my-picture-is-uploaded-to-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!