imnishchhal
imnishchhal

Reputation: 23

How to read array data from the firestore using kotlin?

I am new in android development and when I read array data from firestore using following code

val variable = arrayOf(document.get("restaurant"))

and then loop over the variable using code

varibale.forEach {
    Log.d("someTag", ${it.toString()} + " is your data")
}

I get the result with square brackets at log as following
[somedata, somedata2] is your data

my problem is that forEach loop runs only once and I am not able to get the result (without square brackets) as following

somedata is your data
somedata2 is your data

I have 2 elements in my restaurant array in firestore I will be very thankfull to any one who will help me.

Upvotes: 1

Views: 2632

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317467

arrayOf doesn't parse an array. It creates a new array using the elements you pass to it. That's not what you want. You should instead cast document.get("restaurant") to the type that you expect to get from Firestore.

If a field is an array of strings, then the SDK will give you a List<*>, and you will need to make sure each item in the list is a String, if that's what you stored in the array.

val variable = document.get("restaurant") as List<*>
// Iterate variable here, make sure to check or convert items to strings

Upvotes: 0

Luiz Rodrigo
Luiz Rodrigo

Reputation: 535

You are actually wrapping an array/list into another array when using arrayOf, that's why you see those brackets. Instead, try casting your document.get("restaurant") and then looping directly through it.

Upvotes: 1

Related Questions