2020/04/08 Firebase Cloud Firestore を使ってデータを追加・削除・取得する No6
suspend fun fetchTask(limit: Long): List<Task> {
try {
val collection = database.collection("CollectionName")
val documents = collection.limit(limit).get().documents
return documents.map { it.data }.mapNotNull { it?.toTask() }
} catch (e: Exception) {
return listOf()
}
}
fun Map<String, Any>.toTask(): Task {
val id = this["id"] as String
val time = this["time"] as Long
val name = this["name"] as String
return Task(id, time, name)
}