0

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?

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

});

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.