I am trying to get data from a DataSnapshot from firebase. I can see the DataSnapShot in logs but I am getting an error when assigning it to a variable. See below for more details
In logcat:
D/loggxxxxxxxxxxxxxxxxx: DataSnapshot { key = latitude, value = -20.0688881 }
D/loggxxxxxxxxxxxxxxxxx: DataSnapshot { key = longitude, value = 57.5226424 }
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists()){
Log.d("loggxxxxxxxxxxxxxxxxx",dataSnapshot.toString());
Toast.makeText(MapsActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MapsActivity.this, "Nodata", Toast.LENGTH_SHORT).show();
}
My Firebase Realtime database looks like this:
"location": {
"HarryMaguire": {
"latitude": "-20.0688881",
"longitude": "57.5226424"
},
"RoseGilbert": {
"latitude": "20.3484",
"longitude": "57.5522"
},
CodePudding user response:
This is my approach to pull data from firebase database. I think you are having a problem with data extraction. And this kotlin
fun getDataFromFirestore() {
val userID = sharedPreferences.getString("Firma_Firebase_ID", "")
if (userID != "") {
val DB = Firebase.firestore
DB.collection("Database").document(userID.toString()).addSnapshotListener{ snapshot, error ->
val document = snapshot?.data
runBlocking {
launch {
if (snapshot != null && document != null) {
val name = document.get("name") as? String
if (name != null) {
Name = name
}
}
}
}
//Notify
}
}
}
CodePudding user response:
It looks like you're attaching a ChildEventListener
to the location/HarryMaguire
path in your database. When you do that, onChildAdded
gets trigger with each child property of location/HarryMaguire
, which is why you see logs for latitude
and longitude
.
Will update more, once the question provides the necessary information.