I am developing an Android application using Firebase Realtime Database.
According to Firebase document,
Pass a custom Java object, if the class that defines it has a default constructor that takes no arguments and has public getters for the properties to be assigned.
So I made a custom Kotlin data class with Instant
field as below :
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
@Serializable
data class UserStatus(
// ...
val updatedAt: Instant = Clock.System.now(),
// ...
}
It has default constructor and only public getters.
val newUserStatus = UserStatus()
Firebase.database.reference.child(REFERENCE_USER_STATUS_NAME).child(createdUser.uid).setValue(newUserStatus).await()
But, because of Instant
field of the class, the error occurs as below :
com.google.firebase.database.DatabaseException: Invalid key: value$kotlinx_datetime. Keys must not contain '/', '.', '#', '$', '[', or ']'
at com.google.firebase.database.core.utilities.Validation.validateWritableKey(Validation.java:123)
at com.google.firebase.database.core.utilities.Validation.validateWritableObject(Validation.java:106)
at com.google.firebase.database.core.utilities.Validation.validateWritableObject(Validation.java:107)
at com.google.firebase.database.DatabaseReference.setValueInternal(DatabaseReference.java:283)
at com.google.firebase.database.DatabaseReference.setValue(DatabaseReference.java:159)
//...
When I comment that Instant
field and build, It works well.
What am I missing?
Instant
has a default serializer.
I figured out that Firebase uses an internal property of kotlinx.datetime.Instant
so the key includes $
.
When I asked the same question on kotlinx.datetime
repository, the maintainer answered me he thinks this problem is related to the Firebase, not Instant
.link