Reputation: 173
This is where I am trying to save it to the Firebase Firestore in log I can see it is posted but on the Firebase console, it always shows null.
These are the data class
data class Users(
val name: String?="",
val email:String?="",
val phone: String?="",
val avatar: String?=null,
val user_id:String?=""
)
data class UserLocation(
var geo_location: GeoPoint? = null,
@ServerTimestamp
var timestamp: Date? = null,
var users: Users? = null
)
BUT IN FIRESTORE IT IS ALWAYS POSTED AS NULL
**This is the user location const value **
const val USER_LOCATION = "user locations"
These are the places from where userlocation are been updated
private fun getDeviceLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {
fusedLocationProviderClient.lastLocation.addOnCompleteListener { task ->
val location = task.result
if (location == null) {
requestNewLocationData()
} else {
val geoPoint = GeoPoint(location.latitude, location.latitude)
Log.d(TAG, "UserLocation" + geoPoint.latitude)
Log.d(TAG, "UserLocation" + geoPoint.longitude)
userslocation.geo_location = geoPoint
userslocation.timestamp = null
saveUserLocation()
}
}
} else {
Toast.makeText(requireContext(), "Turn on location", Toast.LENGTH_LONG).show()
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
This is to get the user details
private fun getUserDetails() {
firestoreDatabase = FirebaseFirestore.getInstance()
auth = FirebaseAuth.getInstance()
val userrefd = firestoreDatabase.collection(USER_DETAILS)
.document(auth.uid.toString())
userrefd.get().addOnSuccessListener {
val user = it.toObject(Users::class.java)
Log.i("firebaseImp", "Got value ${it.data}" + user)
userslocation.users = user
}
.addOnFailureListener {
Log.e("firebaseImp", "Error getting data", it)
}
}
Upvotes: 0
Views: 102
Reputation: 138824
To be able to read the correct User data in Firestore, you have to use the correct operations. So you'll need to change:
// π
private fun getUserDetails(userLocation: UserLocation) {
firestoreDatabase = FirebaseFirestore.getInstance()
auth = FirebaseAuth.getInstance()
val userrefd = firestoreDatabase.collection(USER_DETAILS)
.document(auth.uid.toString())
userrefd.get().addOnSuccessListener {
val user = it.toObject(Users::class.java)
Log.i("firebaseImp", "Got value ${it.data}" + user)
userslocation.users = user
//val locationRef = ... //π
locationRef.set(userLocation).addOnCompleteListener(/*.../*); //π
}
.addOnFailureListener {
Log.e("firebaseImp", "Error getting data", it)
}
}
And when you call the above method call it by passing the userLocation
object as an argument:
getUserDetails(userLocation)
To always be sure that you're using the exact same object.
Upvotes: 1