0

Very newbie question, I got this function

private fun getUrl() {

        val storage = Firebase.storage
        val storageRef = storage.reference
        val gsReference = storageRef.child("bucket path")
        val imageFolderReference = gsReference.child("images")

        imageFolderReference.child("images/IMG.jpg")
            .downloadUrl.addOnSuccessListener {
                val url: String = it.toString()
            }
    }

it should return the URL of an image stored on a firebase cloud. How can I expose that url string outside of the functions tho? let's say to set a text view's text to that link.

Thanks in advance

Edit

I tried this

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = DataBindingUtil.inflate<FragmentTestBinding>(inflater, R.layout.fragment_test, container,false)
        val textView: TextView = binding.textView
        getUrl {
            textView.text = it
        }
        return binding.root
    }

    private fun getUrl(callback: (String) -> Unit) {
        val storage = Firebase.storage
        val storageRef = storage.reference
        val gsReference = storageRef.child("gs path")
        val imageFolderReference = gsReference.child("images")
        imageFolderReference.child("images/IMG.jpg")
            .downloadUrl.addOnSuccessListener {
                val url = it.toString()
                callback.invoke(url)
            }

    }

still the text view is empty..

And the callback never gets called..

here's the error logcat

2021-04-06 16:47:45.842 16149-16149/? E/m.pdl.giftpixe: Unknown bits set in runtime_flags: 0x8000
2021-04-06 16:47:46.978 16149-16149/com.pdl.giftpixel E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
2021-04-06 16:47:46.990 16149-16235/com.pdl.giftpixel E/Perf: Fail to get file list com.pdl.giftpixel
2021-04-06 16:47:46.991 16149-16235/com.pdl.giftpixel E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2021-04-06 16:47:46.991 16149-16235/com.pdl.giftpixel E/Perf: Fail to get file list com.pdl.giftpixel
2021-04-06 16:47:46.991 16149-16235/com.pdl.giftpixel E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Osama Omar
  • 79
  • 1
  • 6
  • 3
    https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value – a_local_nobody Apr 06 '21 at 13:12
  • @a_local_nobody it still didn't work for me – Osama Omar Apr 06 '21 at 14:29
  • And have you checked that the code inside your success listener is getting called? What is the value of `url` etc? – Henry Twist Apr 06 '21 at 14:37
  • @HenryTwist checking – Osama Omar Apr 06 '21 at 14:42
  • @HenryTwist edited the question with the logcat – Osama Omar Apr 06 '21 at 15:25
  • It looks like the logs you've posted are just warnings, unrelated to your app. Have you tried debugging the line `callback.invoke(url)` to see what the value of `url` is? – Henry Twist Apr 06 '21 at 15:26
  • If the callback never gets called like you said, then you should be adding a completion listener instead of a success listener. Otherwise you'll get no information on why it's failing. – Henry Twist Apr 06 '21 at 15:36
  • 1
    To determine the download URL, the client needs to make a call to the server. Since this takes time, your main code continues while that call is happening. Then when the download URL is available, the client calls your callback function where right now do `val url: String = it.toString()`. Bottom line of this is that *any code that needs the download URL needs to be inside the `addOnSuccessListener ` block or be called from there*. Also see my answer here: https://stackoverflow.com/1/38819580 (which covers this for JavaScript) and https://stackoverflow.com/a/57040546/209103 (for Android). – Frank van Puffelen Apr 06 '21 at 15:37

0 Answers0