How do I get the document ID for a Firestore document using kotlin data classes

前端 未结 6 1286
慢半拍i
慢半拍i 2020-12-13 11:22

I have kotlin data class

data class Client(

    val name: String = \"\",
    val email: String = \"\",
    val phone: String =\"\") {
constructor():this(\"         


        
6条回答
  •  春和景丽
    2020-12-13 11:51

    I just fetch an id from Firestore before and set it as a field on my object before I create it in Firebase:

        override fun addTodo(todo: Todo) =
        Completable.fromAction {
            val id = firestore
                .collection(COLLECTION_TODOS)
                .document()
                .id
    
            val newTodo = todo.copy(id = id)
    
            firestore
                .collection(COLLECTION_TODOS)
                .document(id)
                .set(newTodo)
        }
    

提交回复
热议问题