Home > Software engineering >  Kotlin "Destructuring declaration initializer of type String must have a 'component1()
Kotlin "Destructuring declaration initializer of type String must have a 'component1()

Time:06-29

This is how I am calling the function :-

val (ImageUrl: String) = uploadImage("ImageTitle", imageTitleUri!!)

This is the function code:-

private fun uploadImage(ImageName: String, ImageUri: Uri): String {
        val fileRef = storagePicRef!!.child("$ImageName.jpg")
        val uploadTask: StorageTask<*>
        uploadTask = fileRef.putFile(ImageUri)

        var imageURL = ""


        uploadTask.continueWithTask(com.google.android.gms.tasks.Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            return@Continuation fileRef.downloadUrl
        }).addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val downloadUrl = task.result
                imageURL = downloadUrl.toString()
            }
            Toast.makeText(baseContext, "Uploaded successfully.", Toast.LENGTH_SHORT).show()
        }
        return imageURL
    }

This is how I am trying to get the Image Url. But I am getting this error:-

"Destructuring declaration initializer of type String must have a 'component1()' function"

I saw other similar questions but here the return value is only one of type string. So, I can directly use it.

CodePudding user response:

delete those parenthesis (ImageUrl: String) and it will work fine,

So your function call should look like this:

val ImageUrl: String = uploadImage("ImageTitle", imageTitleUri!!)

Also it's recommended to start a variable with a lowercase letter so name it imageUrl instead of ImageUrl.

  • Related