简体   繁体   中英

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

I want to get data from the cloud firestore and have to store it to a string variable but unable to do so and need help.

This is below code

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)
            }

getting this from database and I want Link to store to variable 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}

And I want to store it in variable

val first_image : String

first_image = document.data.link

when I use this I am getting an error

and put the variable type var so you can modify value. Val only allowed to set value permenant to that variable.

private var first_image: String

and get image in your variable :

first_image = document.getString("link");

Read Difference between val and var .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM