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 filter data in kotlin based on item

this is method where i am getting data month list but when i try to filter with two filed i am getting compile error

  override fun onGraphDataLoaded(data: List<GraphData>?) {
    Log.d("DATAITEM", data?.size.toString())
    var childList: List<GraphData>? = data?.let { it.filter { s -> s.isChild && t -> t.beginningTime>0 } }
    var parentList: List<GraphData>? = data?.let { it.filter { s -> !s.isChild } }
    Log.d("DATAITEM", "ChildSize"+childList?.size)
    Log.d("DATAITEM", "ParentSize"+parentList?.size)

}

this is my model class

data class GraphData(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    var beginningTime: Long? = 0L,
    var endTime: Long? = 0L,
    var isChild: Boolean
)

i want to filter data based on ischild and beginningTime>0 but it show compile time error please help me in this .

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’re using both s and t as lambda parameters. You can’t change the name of the lambda parameter in the middle of an expression!

Also, you’re comparing a nullable property to an int. You need to take care of its nullability.

Thirdly, there’s no need for .let – that makes the line more complicatted. Just filter directly.

var childList = data?.filter { it.isChild && (it.beginningTime ?: 0) > 0 }
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