I'm trying to get an information ('firstDate') saved in firebase using this code:
if(currentUser != null){
val ref = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().currentUser!!.uid)
val menuListener = object : ValueEventListener {
override fun onCancelled(databaseError: DatabaseError) {
// handle error
}
@SuppressLint("SetTextI18n", "LongLogTag")
override fun onDataChange(dataSnapshot: DataSnapshot) {
user = dataSnapshot.getValue(User::class.java)
Log.i("************************* before Assignement", myFirstDay)
myFirstDay = user?.firstDate
Log.i("************************* after Assignement", myFirstDay)
}
}
ref.addValueEventListener(menuListener)
}
getFirstDayReg()
Log.i("************************* myFirstDayReg", myFirstDay)
The problem: anything before ref.addValueEventListener(menuListener) returns the correct answer but this one :
Log.i("************************* myFirstDayReg", myFirstDay)
It does not giving me anything (no return / nothing at all )
Please help to get the value user?.firstDate and assigned in
myFirstDay
because I need to use it later on my code.
According to Mr. Alex answer i made this code but i still have the same problem nothing changed
interface MyCallback {
fun onCallback(value: String)
}
fun readData(myCallback: MyCallback){
if(currentUser != null){
val ref = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().currentUser!!.uid)
val menuListener = object : ValueEventListener {
override fun onCancelled(databaseError: DatabaseError) {
// handle error
}
@SuppressLint("SetTextI18n", "LongLogTag")
override fun onDataChange(dataSnapshot: DataSnapshot) {
user = dataSnapshot.getValue(User::class.java)
Log.i("************************* before Assignement", user?.firstDate!!)
myFirstDay = user?.firstDate!!
myCallback.onCallback(user?.firstDate!!)
}
}
ref.addListenerForSingleValueEvent(menuListener)
}
}
readData(object: MyCallback {
override fun onCallback(value: String) {
Log.d("TAGTAGTAGTAGTAGTAGTAG", user?.firstDate)
}
})