Home > Back-end >  How to Convert Uri? data type to Uri
How to Convert Uri? data type to Uri

Time:09-23

I am initializing uri so that i can access it from anywhere

private var myuri:Uri? = null

now in Oncreate Method

Oncreate{
   button.setOnClickListner{
   uploadfile(filename, myuri)
   }
}
fun uploadfile(filename:String, fileuri:uri){
  .
  .
  .//some code
}

there is an error says "Type Mismatch required Uri found Uri?" I cannot change anything from uploadfile function and i have to access myuri throughout this kotlin file.

please suggest me something. Thankyou

CodePudding user response:

myuri?.let{ uploadfile(filename, it)

This Code works

CodePudding user response:

What is supposed to happen if it is still null?

If you are SURE it cannot be null the moment you are calling it, just use:

myuri!!

If you want it to throw an exception, use:

myuri ?: throw Exception

Otherwise if you want nothing to happen, when its null, as already suggested by yourself, use:

myuri?.let{...}
  • Related