Is there a Kotlin equivalent for this Swift code to load a collection from FirebaseFirestore?

Solution for Is there a Kotlin equivalent for this Swift code to load a collection from FirebaseFirestore?
is Given Below:

In a working Xcode project, I have the following code:

class ViewController: UIViewController {

let tournamentsRef = Firestore.firestore().collection("tournaments")
var tournaments = [Tournament]()

override func viewDidLoad() {
    super.viewDidLoad()

    tournamentsRef
        .addSnapshotListener { querySnapshot, error in
            guard let documents = querySnapshot?.documents else { return }

            self.tournaments = documents.compactMap { document in
                let tournament = try? document.data(as: Tournament.self)
                return tournament
            }
        }
}

This successfully loads the tournaments from Firestore and updates whenever there are changes.

I’m trying to do the same in Android Studio / Kotlin, but I can’t find the right functions to do it.

I found this, but it’s not a listener. It only loads it once. I tried to make it work without success. It seems the toObjects function is not available for QueryDocumentSnapshots.

val tournamentsRef = FirebaseFirestore.getInstance().collection("tournaments")

tournamentsRef.get().addOnCompleteListener {
    if (it.isSuccessful) {
        tournaments = it.result!!.toObjects(Tournament::class.java)
    } else {
        Log.d(TAG, "Error: ${it.exception!!.message}")
    }
}

I also found this:

val tournamentsRef = FirebaseFirestore.getInstance().collection("tournaments")

tournamentsRef.addSnapshotListener { snapshot: QuerySnapshot?, error ->
    if (error != null) {
        [email protected]
    }
    
    for (document in snapshot!!) {
        val tournament = document.toObject(Tournament::class.java)
        tournaments.add(tournament)
    }
}

Basically, I’m trying to avoid document.getString('variable') for each and every property.

Am I doing something wrong or is this even possible?

After searching some more, I came across something useful and found success combining the two:

val tournamentsRef = FirebaseFirestore.getInstance().collection("tournaments")
var tournaments = ArrayList<Tournament>()

tournamentsRef.addSnapshotListener { snapshot: QuerySnapshot?, error ->
    if (error != null) {
        [email protected]
    }

    tournaments = snapshot!!.toObjects(Tournament::class.java) as ArrayList<Tournament>
}

I had tried this (or something similar) before, but it failed because my data class Tournament had an enum, which I can write, but can’t seem to read. That will be my next question.