Skip to content

"No properties to serialize found on class" error when creating new document with custom class #2810

@Tom-Horvat

Description

@Tom-Horvat
  • Android Studio version:4.2.2
  • Firebase Component: Firestore
  • Component version: firebase-bom:28.2.0

When trying to write a custom object to the Firestore the error occurs. When trying to write it as a HashMap the request is successful. Using the example from the tutorial

Steps to reproduce:

  1. define a custom data class
  2. initialize the object and add a new document to the Firestore firestore.collections("test").add(user)

Relevant Code:

Custom class

data class User(
    val firstName: String? = null,
    val lastName: String? = null,
    val email: String? = null
){
    fun toMap(): HashMap<String, Any?>{
        return hashMapOf(
            "FirstName" to  firstName,
            "LastName" to  lastName,
            "Email" to email
        )
    }
}

Creating an object

val user = User(
            "John",
            "Doe",
            "john.doe@email.com",
        )
        ds.createUser(user)

This fails

fun createUser(user: User) {
        Log.v(TAG, "createUser")

        firestore.collection("test").add(user)
            .addOnSuccessListener {
                Log.v(TAG, "Writing to database successfull")
            }
            .addOnFailureListener { e ->
                Log.w(TAG, "ERROR: ", e)
            }
    }

This succeeds

fun createUser(user: User) {
        Log.v(TAG, "createUser")

        firestore.collection("test").add(user.toMap())
            .addOnSuccessListener {
                Log.v(TAG, "Writing to database successfull")
            }
            .addOnFailureListener { e ->
                Log.w(TAG, "ERROR: ", e)
            }
    }

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions