-1

I am new to the firebase database and is still learning a few things about it. I am trying to pull out data from the real-time database and I am having a hard time doing it.

my database layout is like this.

users 
      --UId
           --firstname
           --lastname
           --dateofbirth
           --mobile
           --email
           --type

my user model

data class User(val uid: String? = "",
                val Firstname:String = "",
                val Lastname:String = "",
                val Mobile:String ="",
                val Dateofbirth:String ="",
                val email:String = "",
                val type: String = ""
){
    constructor(): this ("","","","","","","")
}

I am trying to pull out firstname, lastname, mobile, dateofbirth and email and attach it to my profile textfields for them fname,lname,dob,email

This is the code I am working with

 private fun getUserInfo() {
        val uid = FirebaseAuth.getInstance().currentUser!!.uid
        val ref = FirebaseDatabase.getInstance().reference
        val uidRef = ref.child("users").child(uid)
        val valueEventListener: ValueEventListener = object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
            for (I in dataSnapshot.children){



            }

            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.d("error", databaseError.message)

            }

        }
        uidRef.addListenerForSingleValueEvent(valueEventListener)
    }

How do I pull out that information?

Thanks for the help!

Bilal dar
  • 3
  • 3

1 Answers1

0

It is recommended to use camel case naming scheme for property names like this:

val firstName: String = " "
val secondName: String = " "

You can pull user info like this:

 private fun getUserInfo() {
        val uid = FirebaseAuth.getInstance().currentUser!!.uid
        val ref = FirebaseDatabase.getInstance().reference
        val uidRef = ref.child("users").child(uid)
        val valueEventListener: ValueEventListener = object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                val user = dataSnapshot.getValue(User::class.java)
                //user.firstName
                //user.lastName
                //...
               
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.d("error", databaseError.message)

            }

        }
        uidRef.addListenerForSingleValueEvent(valueEventListener)
    }
Varsha Kulkarni
  • 1,389
  • 2
  • 13
  • 17
  • Link not working – Bilal dar Feb 18 '21 at 10:17
  • can't find anything on how I am supposed to filter that info and attach it into the user profile area of the UI on my android application user?.firstName user?.lastName user?.email user?.Mobile user?.dateOfBirth it also coming out like this. – Bilal dar Feb 18 '21 at 17:14