Ornitologo
Ornitologo

Reputation: 39

Kotlin Server Ktor Exposed: how to nullable optional Fields

I have this function on my Ktor + Exposed App.

override suspend fun createNewCourse(course: CourseModel): Flow<CourseModel> {
    transaction {
        CoursesTable.insert {
            it[requiredCourseId] = course.requiredCourse?.id!!
            it[category] = course.category?.id!!
            it[isPopular] = course.isPopular == true
            it[position] =  course.position
            it[nameEN] = course.name.en
            it[warningEN] = course.warning.en
        }
    }

It doesn't compile. Sometimes some variables (like "warningEN") can be null and I dont want to insert nothing for this field.

How to make it?

Type mismatch. Required:TypeVariable(S) Found: String?

Upvotes: 1

Views: 855

Answers (1)

Ornitologo
Ornitologo

Reputation: 39

My solution:

it[position] =  course.position?.let { course.position!! } ?: null

Upvotes: 2

Related Questions