Ghost
Ghost

Reputation: 407

Firestore only saves certain fields from a custom object

I am trying to save a custom object to Firestore, but for some reason it is not working properly:

abstract class AbstractModel(open val id: String) {}
class ConcreteModel(override val id: String, private val username: String, private val password: String) : AbstractModel(id) {}

fun save() {
  val newModel = ConcreteModel("id", "username", "password")
  val db = Firebase.firestore
  db.collection("collection").document(newModel.id).set(newModel)
}

It only saves the id and nothing else.

Upvotes: 1

Views: 250

Answers (1)

Henry Twist
Henry Twist

Reputation: 5980

I would expect that Firestore won't persist private fields, so I would suggest keeping their visibility public.


A way to still keep your fields private would be to persist the data using a map, for example:

hashMapOf(
    "id" to newModel.id,
    "username" to newModel.username,
    "password" to newModel.password
)

Upvotes: 1

Related Questions