Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get array data from Firestore? Android Kotlin

I have an array in Firestore. How can I get array items? I need streamers array type of string.

What I try is:

firebaseFireStore.collection("Agencies")
        .addSnapshotListener { snapshot, e ->
            if (e == null) {
                val documents = snapshot?.documents
                if(documents != null) {
                    val list = mutableListOf<Ajanslar>()
                    for (document in documents) {

                        val agencyName = document.get("agencyName").toString()
                        val coverImage = document.get("coverImage").toString()
                        val owner = document.get("owner").toString()
                        val platform = document.get("platform").toString()
                        val streamers = document.get("streamers")
                        val newAjans = Ajanslar(agencyName,coverImage,owner,platform,streamers)
                        list.add(newAjans)
                    }
                    ajansListRepo.value = list
                }
            }
        }

Streamers give error and says Type mismatch. Required: kotlin.collections.ArrayList<String> /* = java.util.ArrayList<String> */ Found: Any?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My firestore is like that:
enter image description here

I found an answer like that in Java but I couldn’t do it.

rootRef.collection("products").document("06cRbnkO1yqyzOyKP570").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            ArrayList<String> list = (ArrayList<String>) document.get("ip_range");
            Log.d(TAG, list.toString());
        }
    }
}

});

>Solution :

I wrote that answer and the solution is quite simple in Kotlin. As I see in your screenshot, the streamers field is an array of strings. When you call DocumentSnapshot#get(), the type of object that is returned is Any and not a List<String>. If you need that, you have to explicitly perform a cast to such an object. So please change the following line of code:

val streamers = document.get("streamers")

Into:

val streamers = document.get("streamers") as List<String>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading