Undefined class 'StorageTaskSnapshot'. Try changing the name to the name of an existing class or creating a class with the name 'StorageTaskSnapshot'

I want to upload a photo to the firebase. When I tried to use StorageTaskSnapshot it makes underline StorageTaskSnapshot saying Undefined class 'StorageTaskSnapshot'.

This is my code:

Future<String> uploadPhoto(img) async {
    UploadTask task =
        FirebaseStorage.instance.ref().child("Posts Pictures").child("post_$postId.jpg").putFile(img);
    StorageTaskSnapshot storageTaskSnapshot =await task.onComplete;
    String downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
    return downloadUrl;
  }

Anyone Please Help me.

1 answer

  • answered 2021-06-23 07:39 Peter Haddad

    Try the following:

    Reference ref = FirebaseStorage.instance.ref();
    TaskSnapshot uploadFile = await child("Posts Pictures").child("post_$postId.jpg").putFile(img);
      if (uploadFile.state == TaskState.success) {
                final String downloadUrl = await snapshot.ref.getDownloadURL();
             }
    

    First get the reference to the firebase storage instance, then using putFile() you can upload the file to the storage which also returns a TaskSnapshot. After that if the state of the uploaded file is success then get the downloaded url.