dimanche 8 décembre 2019

Collapse if-else statement with assignment

I am trying to do a null/empty check-in a conditional block as seen below:

However, the results are kind of bizzare in that the block with !filter.isNullOrEmpty() should not execute since the filter is null. How does isNullOrEmpty() usually work, and how and when to use it?

I just have a simple set up like:

val filter : String? = session.parameters["filter"]?.get(0).toString()

            val rootJsonObject = JsonObject()
            val gson = GsonBuilder().setPrettyPrinting().create()

            val list  = if (!filter.isNullOrEmpty()) {
                KOKONetApp.database.filmDao().getByRegionFilter(filter)
            } else {
                KOKONetApp.database.filmDao().getByRandom()
            }

Aucun commentaire:

Enregistrer un commentaire