Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get one value from a data class as a list

I’ve been developing an Android forum app in Kotlin, using Jetpack Compose. I’d like to send notifications to those who have replied to some specific post.

First, I’d like to get the uids and tokens of them(fun getOthersToken), and then send notifications to them(fun createNotification). However, I cannot get each uid of them and I have an error because "uid" in (uid in thoseWhoRepliedEntity.uid) is Char type, not String.
This is the current code.

var thoseWhoReplied = mutableListOf<ThoseWhoRepliedEntity>()

val service: UserService = retrofit.create(
    UserService::class.java
)

val apiResponse = service.getOthersToken(token, postId)
    .execute().body()
    ?: throw IllegalStateException("body is null.")

for (info in apiResponse) {

    val thoseWhoRepliedEntity = ThoseWhoRepliedEntity(
        info.uid,
        info.token
    )

    thoseWhoReplied.add(thoseWhoRepliedEntity)

    
    try {
        val service: NotificationService =
            retrofit.create(NotificationService::class.java)

        for (uid in thoseWhoRepliedEntity.uid) {
            
            val notification = NotificationEntity(
                uid,
                "You have a reply.",
                "${LocalDateTime.now()}"
            )

            service.createNotification(token, notification)
                .enqueue(object : Callback<ResponseBody> {
                    override fun onResponse(
                        call: Call<ResponseBody>,
                        response: Response<ResponseBody>
                    ) {
                        Log.d(
                            "Response is",
                            "${response.body()}"
                        )
                    }

                    override fun onFailure(
                        call: Call<ResponseBody>,
                        t: Throwable
                    ) {
                        Log.d("Hi", "error")
                    }
                })
        }

ThoseWhoRepliedEntity

data class ThoseWhoRepliedEntity(
    var uid: String = "",

    var token: String = ""
)

Could someone help me? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are getting Char elements and not a expected String because you are iterating over content of your uid strings (thus getting its characters), so you are one step too deep. So instead of:

for (uid in thoseWhoRepliedEntity.uid) {
    
    val notification = NotificationEntity(
        uid,
        "You have a reply.",
        "${LocalDateTime.now()}"
    )
    ...
}

you need to write:

for (entity in thoseWhoReplied) {

    val notification = NotificationEntity(
        entity.uid,
        "You have a reply.",
        "${LocalDateTime.now()}"
    )
    ....
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading