Home > Blockchain >  Convert Firestore document to list of objects
Convert Firestore document to list of objects

Time:05-02

I have this Firestore document Quiz_android that looks list this:

enter image description here

It is a simple array with maps in it. Now I would like to bind those results to some objects in Kotlin. Therefore I have made the following:

data class QuizBody(
    val questions: List<Question>
)

data class Question(
    val question: String,
    val answers: List<String>,
    val answer: Int
)

A Quizbody is just all the questions for the quiz in a list, and in that list, I have classes of Question which should be able to store all the data from the call.

But how do I bind the result from the call to those objects?

    suspend fun getQuestions(quizToGet: String) {
        try {
            //firestore has support for coroutines via the extra dependency we've added :)
            withTimeout(5_000) {
                firestore.collection("Quizzes").document(quizToGet).get()
                    .addOnCompleteListener { task ->
                        if (task.isSuccessful) {
                            val result = task.result
                            if (result.exists()) {

                                val myObject = result.toObject(QuizBody::class.java)
                                println(myObject)
                            }
                        }
                    }
            }
        } catch (e: Exception) {
            throw QuizRetrievalError("Retrieving a specific quiz was unsuccessful")
        }
    }

I have made this but this does not work.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hva.madlevel7task2, PID: 3995
    java.lang.RuntimeException: Could not deserialize object. Class com.hva.madlevel7task2.model.QuizBody does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped

Edit:

I have updated the data class:

data class QuizBody(
    var questions: List<Question>? = null
)

data class Question(
    var question: String? = null,
    var answers: List<String>? = null,
    var answer: Int? = null
)

suspend fun getQuestions(quizToGet: String) it still the same, now I get this in the console:

I/QuizViewModel: function: getListQuestions
W/Firestore: (24.1.1) [CustomClassMapper]: No setter/field for Questions found on class com.hva.madlevel7task2.model.QuizBody (fields/setters are case sensitive!)
I/System.out: QuizBody(questions=null)

CodePudding user response:

The following error:

java.lang.RuntimeException: Could not deserialize object. Class com.hva.madlevel7task2.model.QuizBody does not define a no-argument constructor.

Is very straightforward in my opinion. Your class "QuizBody" does not have a no-argument constructor. When you try to deserialize an object that comes from a Firestore database, the Android SDKs require that the class should mandatorily have a default no-argument constructor.

In Kotlin, the data classes don't provide a default no-arg constructor if all the properties of the class are declared with val. For such properties, Kotlin requires that their values be specified in the constructor since they can't possibly change later. So this is required because we need to ensure the compiler that all the properties have an initial value. You can provide to all of the properties an initial value of null or any other value you find more appropriate. So your classes should look like this:

data class QuizBody(
    var questions: List<Question>? = null
               
  • Related