Kotlin Multiplatform Academy · 课时

共享模型中的空值安全

使用 Kotlin 可空性防止跨平台崩溃

第 3 / 4 课13 个步骤

共享模型中的空值安全 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Multiplatform Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Multiplatform Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Crashes You Can Prevent

A missing value is a top cause of app crashes. Kotlin's null safety lets shared models guard against it on both platforms.

Non-Null by Default

A normal type can never hold null. Declaring a field as String guarantees it always has a real value.

data class User(val name: String)

The Nullable Type

Add a question mark to allow null. This marks a field as optional, so the compiler forces you to handle the empty case.

data class User(val name: String, val nickname: String?)

Safe Call Operator

Use ?. to read a nullable field safely. If the value is null, the whole expression yields null instead of crashing.

val length = user.nickname?.length

Provide a Fallback

The Elvis operator ?: supplies a default when the value is null, so downstream code always gets something usable.

val shown = user.nickname ?: "Anonymous"

Default Values in Models

Give a constructor parameter a default so callers can skip optional fields entirely while keeping the model non-null.

data class User(val name: String, val active: Boolean = true)

Smart Casts After a Check

Once you confirm a value is not null, Kotlin smart casts it. Inside the check you use it as a plain non-null type.

if (user.nickname != null) {
  println(user.nickname.length)
}

Avoid the !! Operator

The !! operator forces a value to non-null and crashes if it is null. Reach for safe calls and Elvis instead.

val risky = user.nickname!!.length // can crash

Model Optionality Honestly

Mark a field nullable only when it truly can be absent. Clear types document your domain for both teams.

data class Profile(val bio: String?, val avatar: String?)

Why This Matters Cross-Platform

Null safety lives in commonMain, so Android and iOS inherit the same protection from the very same model. 🙂

One Source of Truth

Because nullability rules sit in shared code, neither app can accidentally treat an optional field as required.

Quick Check

How should you read a nullable field with a fallback?

Recap

Mark optional fields with ?, read them via safe calls and Elvis, and avoid !!, so shared models stay crash-safe on both apps.

免费开始

用 AI 导师学习 Kotlin — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「共享模型中的空值安全」课时是免费的吗?

是的 — 「共享模型中的空值安全」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。

「共享模型中的空值安全」这节课中我会学到什么?

使用 Kotlin 可空性防止跨平台崩溃 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Kotlin Multiplatform Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「共享模型中的空值安全」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?

能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为您的领域建模使用数据类
  2. 使用枚举与密封类表示状态
  3. 共享模型中的空值安全
  4. 模型上的验证辅助工具
← 返回 Kotlin Multiplatform Academy