I'm writing a function that does a query on firestore documents, and returns them in a list. If there are no documents, return null (or an empty list). I'm not very familiar with Kotlin, I only know Flutter
Kotlin code (what I tried) but it's saying I cannot return inside the listener:
fun getPlaces(type: String): List<DocumentSnapshot>? {
db.collection("users")
.whereEqualTo("type", type)
.get()
.addOnSuccessListener { documents ->
if (documents.isEmpty) {
return null
} else {
return documents
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}
}
Here's the Flutter equivalent of what I'm trying to do:
List<DocumentSnapshot> getDocs(String type) async {
QuerySnapshot snaps = await db.collection('users').where('type', isEqualTo: type).getDocuments();
if (snaps.documents.isEmpty) {
return null;
} else {
return snaps.documents;
}
}
Syntax might be a little off but something like that
var docs: List<DocumentSnapshot>? = nulloutside the listener and puttingdocs = documents.documentsinside the listener would not work? I just need to store the documents in a list after doing the query