I am trying to access the FireStore using Kotlin and retrieve all documents from a collection called “userData”.
I think the “addOnSuccessListener” is a coroutine, but I’m using a loop in it to pass the constant “ps” and the constant "us ", assign data to “dataSets”, put the DataClass type data into an array of DataClass type “dataSets”, and " addOnSuccessListener I want to be able to use “dataSets” outside of “addOnSuccessListener” after the “addOnSuccessListener” process is complete.
package com.example.bulogapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.bulogapp.databinding.ActivityRegistrationBinding
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
class RegistrationActivity : AppCompatActivity() {
private lateinit var binding: ActivityRegistrationBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRegistrationBinding.inflate(layoutInflater)
setContentView(binding.root)
val registrationUserNameTextField = binding.registrationUserNameTextField
val registrationSendButton = binding.registrationButton
val backButton = binding.backButton
var dataSet:Array<UserDataInput>
registrationSendButton.setOnClickListener {
val db = Firebase.firestore
var dataSets = listOf<UserDataInput>()
db.collection("userData").get().addOnSuccessListener { documents ->
for (document in documents) {
val ps = document["pass"].toString()
val us = document["userName"].toString()
dataSets += UserDataInput(pass = ps, userName = us)
}
}
}
The contents of DataCLass are as follows
package com.example.bulogapp
data class UserDataInput(
var pass:String,
var userName:String
)
It is possible that I am doing it wrong. I want to eventually get all the documents from the FireStore collection, put the data in them in an array, and be able to use the data in that array after all those processes are done.
Thank you in advance for your help.