共有モデルのNull Safety
Kotlinのnullabilityでクロスプラットフォームのクラッシュを防ぎます
「共有モデルのNull Safety」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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?.lengthProvide 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 crashModel 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.
よくある質問
「共有モデルのNull Safety」レッスンは無料ですか?
はい。「共有モデルのNull Safety」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「共有モデルのNull Safety」で何を学びますか?
Kotlinのnullabilityでクロスプラットフォームのクラッシュを防ぎます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「共有モデルのNull Safety」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ドメイン用のData Classes
- 状態を表すEnumsとSealed Classes
- 共有モデルのNull Safety
- モデルのValidation Helpers