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

632

Solution 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.

Solution 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.

Share:
632
Punreach Rany
Author by

Punreach Rany

Updated on December 24, 2022

Comments

  • Punreach Rany
    Punreach Rany over 1 year

    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!