You are currently viewing [Question]Update Local variables from Firebase RealTime Database

[Question]Update Local variables from Firebase RealTime Database

Hello, I am making an online Chess game for Android phones using the Firebase RealTime Database. When a player makes a move on his phone, the program updates the Database with a string describing the state of the Chess board after the move. The update function works fine, as I can see that the string has the correct value from the Firebase Console.

My problem is updating the program running on the other player’s phone, after the move was carried out. In detail, What I am trying to do is get the value of the string from the Database and store it in a local variable. Inside the onChildChanged() function I call a function called updateLocal() which looks like this:

private fun updateLocal() { database.child(matchName).get().addOnSuccessListener { if (it.exists()){ try{ var boardUpdateString: String = it.child("Pieces Set").value as String ChessBoard.update(boardUpdateString) }catch (e: NullPointerException){ println("-0-0-0-0-0---Null---0-0-0-0-0-") } }else{ Toast.makeText(this,"MATCH NOT FOUND ",Toast.LENGTH_SHORT).show() } }.addOnFailureListener{ Toast.makeText(this,"FAILED",Toast.LENGTH_SHORT).show() } } 

When I try to assign the value of the string to boardUpdateString, I get the following error:

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String 

How is possible to fix this? All suggestions are welcome

submitted by /u/ragnaros_preachos
[link] [comments]