Home > Software design >  How to ignore/exclude fields on model on Firestore
How to ignore/exclude fields on model on Firestore

Time:11-23

My current goal is to send some useful data to my Firestore database. The problem is that I keep seeing the ignore fields on it.

I do not have any need to keep the boolean 'isUserAuthenticated' and 'isNewUser' on the database.

For the fields, I do not want to keep I am adding an @Exclude and I am even tried to use @ IgnoreExtraProperties on top of the class.

An except from the model:

@IgnoreExtraProperties
class UserModel : Serializable {
    // authentication logic

    @Exclude
    var isUserAuthenticated = false

This is an excert that shows how I send it:

val profile = UserModel(
            firebaseUser.uid,
            profileName,
            firebaseUser.email,
            profileImage,
            currentLanguage,
            profileLanguages,
            0,
            100
        )

        val uidRef: DocumentReference = firebaseUser.let { usersRef.document(it.uid) }

        uidRef.get().addOnCompleteListener { uidTask: Task<DocumentSnapshot> ->
            if (uidTask.isSuccessful) {
                Log.i(TAG, "createProfileInFirestore: uidTask.isSuccessful()")
                try {
                    val document: DocumentSnapshot = uidTask.result
                    if (!document.exists()) {
                        uidRef.set(profile)
                            .addOnCompleteListener { profileCreationTask: Task<Void> ->

In Java the ignore fields worked but I am currently rewriting it in Kotlin.

CodePudding user response:

The @Exclude annotation added in front of the public field works in Java. In Kotlin, you have to add @get:Exclude like this:

@get:Exclude
var isUserAuthenticated = false
  • Related