簡體   English   中英

Android Kotlin在Firebase數據庫中保存數據類

[英]Android Kotlin save data class in Firebase Database

我正在Kotlin上集成Firebase的Android應用程序上工作。 現在,我要將數據( Kotlin data class )存儲到Firebase數據庫中。

資料類別:

@Parcelize
data class Trip(
    val fromAddress: String,
    val toAddress: String,
    val fromLocation: String,
    val toLocation: String,
    val orderUid: String
) : Parcelable

@Parcelize
data class Order(val trip: Trip, val date: Date, var status: OrderStatus, val userUid: String) : Parcelable {
    var pickUpDate: Date? = null
    var dropOffDate: Date? = null
    var price: Double? = null

}

Fireabase數據庫寫操作:

fun createNewOrder(
    fromAddress: String,
    toAddress: String,
    fromLocation: Location,
    toLocation: Location
) {
    val fromGeoLocation = fromLocation.convertToGeoLocation()
    val toGeoLocation = toLocation.convertToGeoLocation()
    val userUid = sharedPreferences[CURRENT_USER_UID_KEY, ""]!!
    val orderKey = databaseReference.child(DB_ORDERS_KEY).push().key
    val tripKey = databaseReference.child(DB_TRIPS_KEY).push().key
    val trip = orderKey?.let { createNewTrip(fromAddress, toAddress, it) }
    val order = trip?.let { Order(it, Date(), OrderStatus.PENDING, userUid) }

    if (trip != null && order != null && !userUid.isNullOrEmpty()) {
        ordersGeoFire.setLocation(trip.fromGeoLocation, fromGeoLocation)
        ordersGeoFire.setLocation(trip.toGeoLocation, toGeoLocation)
        val allData = mutableMapOf<String, Any>()
        allData["/$DB_TRIPS_KEY/$tripKey"] = trip?.convertToMap()
        allData["/$DB_ORDERS_KEY/$orderKey"] = order?.convertToMap()
        allData["/$DB_USERS_KEY/$userUid/$DB_ORDERS_KEY/$orderKey"] = true
        databaseReference.updateChildren(allData)
    }
}

我收到此錯誤:

com.google.firebase.database.DatabaseException:在類kotlin.Unit上找不到要序列化的屬性

有什么建議么?

您的代碼中的問題是Trip類中的文件未初始化。 建議的創建模型類的方法是:

class Trip(
        val displayName: String = "",
        val email: String = "",
        val photoUrl: String = "",
        val userId: String = ""
)

這只是您所需要的。 創建Trip類的新對象的方法是:

val trip = Trip(displayName, email, photoUrl, userId)

這是我的錯誤,因為我忘記在擴展程序的convertToMap函數中添加return type 現在他們看起來像這樣:

fun Trip.convertToMap(): MutableMap<String, Any> {
    val map = mutableMapOf<String, Any>()
    map["fromAddress"] = fromAddress
    map["toAddress"] = toAddress
    map["fromGeoLocation"] = fromGeoLocation
    map["toGeoLocation"] = toGeoLocation
    map["orderUid"] = orderUid
    return map
}

同時還要感謝@Alex Mamo的回答,這對我的調查很有幫助。

現在我的代碼看起來像這樣,可以正常工作:

@Parcelize
data class Trip(
    var fromAddress: String = "",
    var toAddress: String = "",
    var fromGeoLocation: String = "",
    var toGeoLocation: String = "",
    var orderUid: String = ""
) : Parcelable

@Parcelize
data class Order(
    var trip: Trip? = null,
    var date: Date? = null,
    var status: OrderStatus? = null,
    var userUid: String = ""
) : Parcelable {
    var pickUpDate: Date? = null
    var dropOffDate: Date? = null
    var price: Double? = null
}

fun createNewOrder(
    fromAddress: String,
    toAddress: String,
    fromLocation: Location,
    toLocation: Location
): LiveData<Order> {
    orderLiveData = MutableLiveData()
    orderLiveData.value = null
    val userUid = sharedPreferences[CURRENT_USER_UID_KEY, ""]!!
    val orderKey = databaseReference.child(DB_ORDERS_KEY).push().key
    val tripKey = databaseReference.child(DB_TRIPS_KEY).push().key
    val trip = orderKey?.let { createNewTrip(fromAddress, toAddress, fromLocation, toLocation, it) }
    val order = trip?.let { Order(it, Date(), OrderStatus.PENDING, userUid) }

    if (trip != null && order != null && !userUid.isNullOrEmpty()) {
        val allData = mutableMapOf<String, Any>()
        allData["/$DB_TRIPS_KEY/$tripKey"] = trip.convertToMap()
        allData["/$DB_ORDERS_KEY/$orderKey"] = order.convertToMap()
        allData["/$DB_USERS_KEY/$userUid/$DB_ORDERS_KEY/$orderKey"] = true
        databaseReference.updateChildren(allData) { databaseError, databaseReference ->
            if (databaseError == null) orderLiveData.value = order
        }
    }

    return orderLiveData
}

希望這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM