Skip to content

[FireStore] How to decode Timestamp in Multiplatform Code (Android/iOS) #113

Closed
@wezley98

Description

@wezley98

Given

data class Article(
    val title: String = "",
    val subTitle: String = "",
    val image: String = "",
    val date: Double = 0.0,
    val content: String = ""
)

Firebase.firestore.collection("articles")
            .get()
            .documents
            .map { doc ->
                Article(
                    doc.get("title"),
                    doc.get("subTitle"),
                    doc.get("image"),
                    doc.get("date"),
                    doc.get("content")
                )
            }

When

date is type timestamp in Firestore.

Then

How do we decode this type in commonMain for example? I have tried setting Article.date to String and Double and both crash.

 kotlinx.serialization.SerializationException: Expected Timestamp(seconds=1606944088, nanoseconds=0) to be double

Does the SDK provide any method to help with decoding this automatically?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions