Dkathayat1
Dkathayat1

Reputation: 173

Android kotlin firebase data posted on firebase is always null I am not sure what I am doing wrong here below are the code snippet and screenshots

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.

enter image description here

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

enter image description here

**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

Answers (1)

Alex Mamo
Alex Mamo

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

Related Questions