0Pricing
Dart Academy · Lesson

Nullable Types and the Question Mark

Mark when a value can be null.

Nullable Types and the Question Mark is a free Dart 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 Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Billion-Dollar Bug

Null errors crash apps everywhere. Dart's null safety stops most of them before your program ever runs. ✨

Non-Nullable by Default

By default a Dart variable can never hold null. This int must always contain a real number.

int age = 30;

Null Is Rejected

Try to assign null to a normal type and the compiler refuses. That error appears at edit time, not in production.

int age = null;

Adding the Question Mark

Append ? to a type to allow null. Now the variable may hold a value or hold nothing at all.

int? age = null;

Nullable Means Maybe

A nullable type says this value might be missing. It is a promise to handle the empty case carefully.

String? nickname;

Defaults to Null

An uninitialized nullable variable starts as null automatically, so you can declare it now and fill it later.

double? price;
print(price);

Cannot Use Directly

You cannot call methods on a nullable value as if it were always present. The compiler forces you to check first.

String? name;
print(name.length);

Narrowing With a Check

An if test that rules out null lets Dart promote the type, so inside the block it is safe to use.

if (name != null) {
  print(name.length);
}

Function Returns Too

Return types can be nullable as well. A ? here signals the function might give back nothing.

String? findUser(int id) => null;

Choose the Tighter Type

Prefer non-nullable types whenever a value truly always exists. Reach for nullable only when absence is meaningful.

Safer From the Start

Sound null safety means if a type lacks ?, you can trust it is never null anywhere in your code.

Quick Check

What does the ? after a type mean?

Recap

You learned that Dart types are non-nullable by default, and the ? mark opts a type into possibly holding null. 🎯

Frequently asked questions

Is the “Nullable Types and the Question Mark” lesson free?

Yes — the full text of “Nullable Types and the Question Mark” is free to read here on the web, and the Dart 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 Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Nullable Types and the Question Mark”?

Mark when a value can be null. You practise Dart 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 Dart Academy?

No prior experience is required. Dart 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 “Nullable Types and the Question Mark” 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 Dart Academy lesson?

Yes. Every Dart 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. Arrow Syntax for One-Line Functions
  2. Anonymous Functions and Closures
  3. Nullable Types and the Question Mark
  4. Null-Aware Operators: ?., ??, and !
← Back to Dart Academy