sveggen
sveggen

Reputation: 53

Retrieve document from Firestore as custom object

I am trying to retrieve a document from Firestore as a custom object called ShopModel. When I call the toObject function I get the following error message.

How can I solve this?


ShopStore.kt

override fun getDocument(): List<ShopModel> {
        val firestore = FirebaseFirestore.getInstance()
        firestore.firestoreSettings = FirebaseFirestoreSettings.Builder().build()
        val document = firestore.collection("shops").document("1")
        document.get().addOnSuccessListener { documentSnapshot ->
            val shop = documentSnapshot.toObject<ShopModel>() //toObject 
...

ShopModel.kt

@Parcelize
data class ShopModel (
    var uuid: String?,
    var name: String?
) : Parcelable {constructor() : this("","")}

Upvotes: 0

Views: 421

Answers (1)

Andrew
Andrew

Reputation: 4702

Change val shop = documentSnapshot.toObject<ShopModel>() with val shop = documentSnapshot.toObject(ShopModel::class.java). That should work.

You also don't need to specify documentSnapshot -> and can use it instead. So val shop = it.toObject(ShopModel::class.java)

Also make sure to use Firebase-ktx instead of Firebase. There are a hand of use kotlin-extensions that will make programming easier

Upvotes: 1

Related Questions