0

real i'm not sure if can explain the proplem

1-I need to get posts from realtime_firebase 2-every post has a user_id

then I need to get user Image from Storage_firebase by userId

the problem is return null for image

Code

fun getPosts(liveData: MutableLiveData<List<Post>>) {
        val databaseRef = firebaseDatabase.getReference("Posts")
        var posts: ArrayList<Post> = ArrayList()
        databaseRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                posts.clear()
                for (snapshot in snapshot.children) {
                    var post = snapshot.getValue(Post::class.java)
                    post?.image = getUserImage(post?.userId!!)
                    posts.add(post!!)
                }
                liveData.value = posts
            }
            override fun onCancelled(error: DatabaseError) {
            }
        })
    }
private fun getUserImage(user_id: String): String? {
    var imageUri = ""
    firebaseStorage.reference.child("users").child("profile")
        .child(user_id).downloadUrl.addOnSuccessListener {
      //problem is not access to this methode
            imageUri = it.toString()
        }
    return imageUri
} 
medmjs
  • 86
  • 5
  • So you say that `firebaseStorage` object is null? Did you initialize it? If yes, show us where did you do that. – Alex Mamo Dec 13 '21 at 11:08
  • In that case, firebaseStorage isn't null, right? It's about the `imageUri` being null, correct? – Alex Mamo Dec 13 '21 at 11:17
  • I'am using dagger hilt to initialize Provides Singleton fun getStorage(): FirebaseStorage = FirebaseStorage.getInstance() and it run when upload Image problem is not access to addOnSuccessListener – medmjs Dec 13 '21 at 11:19
  • yes firebaseStorage is Not null put problem not access to addOnSuccessListener – medmjs Dec 13 '21 at 11:24
  • I'm not sure I understand what the problem is. So which object is null? – Alex Mamo Dec 13 '21 at 11:27
  • it's not access to this methode firebaseStorage.reference.child("users").child("profile") .child(user_id).downloadUrl.addOnSuccessListener { //problem is not access to this methode imageUri = it.toString() } – medmjs Dec 13 '21 at 11:31
  • What do you mean by ""problem is not access to this methode? – Alex Mamo Dec 13 '21 at 11:52
  • 1
    Does this answer your question? [Why does my function that calls an API return an empty or null value?](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value) – Tyler V Dec 13 '21 at 13:53

1 Answers1

1

I'm solving the problem but not understand why -using MutableStateFlow to return value

 databaseRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                posts.clear()
                for (snapshot in snapshot.children) {
                    var post = snapshot.getValue(Post::class.java)
                    GlobalScope.launch {
                        getUserImage(post?.userId!!).collect {
                            post?.image = it
                        }
                    }

                    posts.add(post!!)
                }
                liveData.value = posts
            }

            override fun onCancelled(error: DatabaseError) {
            }
        })
    }

    private fun getUserImage(user_id: String): MutableStateFlow<String> {
        var imageUri = MutableStateFlow<String>("")

        var imageRef = firebaseStorage.reference.child("users").child("profile")
            .child(user_id).downloadUrl.addOnSuccessListener {
            GlobalScope.launch {
                imageUri.emit(it.toString())
            }
        }

        return imageUri
    }
medmjs
  • 86
  • 5