簡體   English   中英

如何將來自 firestore 數據庫中特定集合的文檔字段數據存儲到 kotlin 中的字符串變量

[英]How can I store document field data from a particular collection in firestore database to a string variable in kotlin

我想從 Cloud Firestore 獲取數據,並且必須將其存儲到字符串變量中,但無法這樣做並且需要幫助。

這是下面的代碼

val db = FirebaseFirestore.getInstance()
        val docRef = db.collection("SlideShowImages").document("1")
        docRef.get()
            .addOnSuccessListener { document ->
                if (document != null) {
                    Log.d(TAG, "DocumentSnapshot data: ${document.data}")
                } else {
                    Log.d(TAG, "No such document")
                }
            }
            .addOnFailureListener { exception ->
                Log.d(TAG, "get failed with ", exception)
            }

從數據庫中獲取它,我希望Link存儲到變量 first_image

{about=FCB, link=https://firebasestorage.googleapis.com/v0/b/missionx-g6305.appspot.com/o/EVENTS%2FSlide%20Show%20Images%2FCurrent%2FFCB.jpg?alt=media&token=950c5200-c553-4639-b33e-2b91a220b19c}

我想將它存儲在變量中

val first_image : String

first_image = document.data.link

當我使用它時出現錯誤

並放置變量類型var以便您可以修改值。 Val只允許為該變量設置永久值。

private var first_image: String

並在您的變量中獲取圖像:

first_image = document.getString("link");

閱讀val 和 var之間的區別。

暫無
暫無

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

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