1

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.

1
  • 2
    Please edit your question and add your database structure as a screenshot. Commented Aug 5, 2020 at 14:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot friend.. some more info document.get("restaurant") was a ArrayList so I have to cast into ArrayList as follows "val variable = document.get("restaurant") as ArrayList<String>"
0

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

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.