0Pricing
Kotlin Multiplatform Academy · Lesson

Defaults & Clearing Keys

Handle missing values and reset state cleanly.

Defaults & Clearing Keys is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Missing Keys Happen

On a fresh install, no keys exist yet. How your code handles a missing value decides whether the app feels solid or crashes. 🛟

Defaults to the Rescue

The plain getters take a default you supply. If the key is absent, you get that fallback instead of an error.

val theme = settings.getString("theme", "light")

Nullable Getters

Prefer to know if a value was ever set? Use the OrNull getters, which return null when the key is missing.

val theme: String? = settings.getStringOrNull("theme")

Check Before Reading

Ask hasKey whether a value exists. Handy when absence itself means something, like first launch.

if (!settings.hasKey("onboarded")) showOnboarding()

Remove One Key

Delete a single value with remove. The key vanishes and future reads fall back to the default again.

settings.remove("theme")

Minus Operator

There's a shorthand too: the minus operator removes a key just like calling remove.

settings -= "theme"

Clear Everything

Use clear to wipe all keys at once. Great for a sign-out or a reset-to-defaults action.

settings.clear()

Clear Is Powerful

Be careful: clear erases every key in that Settings instance, not just yours. Reserve it for true full resets.

Count the Keys

The size property tells you how many keys are stored, which is useful for diagnostics or tests.

val stored = settings.size

Defaults vs Absence

A default value and a stored value can look identical. When that difference matters, choose the nullable getter over a default.

Reset Cleanly

Wrap reset logic in one shared function so both apps clear the same keys the same way.

fun resetPrefs(settings: Settings) {
    settings.remove("theme")
    settings.remove("username")
}

Quick Check

Let's confirm how to detect a missing value.

Recap

You handle missing keys with defaults or OrNull, check existence with hasKey, and reset state with remove or clear. Next: storing an auth token. 🎉

Frequently asked questions

Is the “Defaults & Clearing Keys” lesson free?

Yes — the full text of “Defaults & Clearing Keys” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defaults & Clearing Keys”?

Handle missing values and reset state cleanly. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defaults & Clearing Keys” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why a Settings Abstraction
  2. Read & Write Typed Values
  3. Defaults & Clearing Keys
  4. Store an Auth Token Securely
← Back to Kotlin Multiplatform Academy