0

So this is my code right now but the app still crashes when I run this:

db.collection("historyCal").document(idData.toString()).get()
    .addOnSuccessListener { document ->
        if (document != null) {
            val calin = document.get("calInDay") as List<*>
            calin.forEach {
                Log.d("someTag",calin.toString())
            }
        } else {
            Log.d("noexist", "No such document")
        }
    }

I have data that consists of:

  1. collection: historyCal
  2. document: id user I already put on variable idData
  3. fields: callInDay, it's an array consist of a number (2000,2100,2000)

I want to get data from callInDay and print each value with a log:

The error log :

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.joshua.testapp, PID: 27966 kotlin.TypeCastException: null cannot be cast to non-null type kotlin.collections.List<*> at com.joshua.testapp.HistoryActivity$setBarChartValues$1.onSuccess(HistoryActivity.kt:45) at com.joshua.testapp.HistoryActivity$setBarChartValues$1.onSuccess(HistoryActivity.kt:18) at com.google.android.gms.tasks.zzn.run(Unknown Source:4) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

line 45 : val calin = document.get("calInDay") as List<*> line 18 : class HistoryActivity : AppCompatActivity() { //i think this is due to line 45

Can someone help me? Thanks

1
  • no one will understand the problem without error logs. Remember to post them with all your questions. Commented Apr 15, 2021 at 7:15

1 Answer 1

1

Assuming that your POJO class is called "Calin", to get the data under "calInDay" array, please use the following lines of code:

db.collection("historyCal").document(idData.toString()).get().addOnSuccessListener { document ->
    if (document.exists()) {
        val calin = document.get("calInDay") as? List<Map<String, Calin>>
        calin.forEach {
            Log.d("someTag",calin.toString())
        }
    } else {
        Log.d("noexist", "No such document")
    }
}

So when you get the data from the "DocumentSnapshot" object, you can extract that array as a List<Map<String, Calin>>. However, if you want to map an array of objects from Cloud Firestore to a List of objects, please check the following article on Medium:

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

7 Comments

thanks, but i still get the same error log.. now, i change the code into this : var calin: List<*>? = document.get("calInDay") as List<*>? calin?.forEach { Log.d("calorie",calin.toString()) } but i didn't get the data
Have you also tried val calin = document.get("calInDay") as? List<Map<String, Calin>>, as in my updated answer? Does it work this way?
yeah, but i still get the same error.. perhaps due to the POJO ?, internal class Calin {var cal: List<String>? = null }
No, that list doesn't contain String objects, it contains Calin objects. It should be: data class CalinPojo(var cal: List<Calin>? = null) and then get val cal = document.toObject(CalinPojo::class.java).cal;, as explained in that article. Does it work now?
oh okay i found out how to do it.. it's kinda simple just val cal = document.get("calInDay") as List<Long> dont need to make a new class.. thanks so much to help me :)
|

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.