0Pricing
Dart Academy · Lesson

Default Values and required

Make parameters optional or mandatory.

Default Values and required 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.

Sensible Fallbacks

A default value is the fallback Dart uses when a caller skips a parameter. It saves callers from passing the obvious choice every time.

Default on Named Parameters

Give a named parameter a default with an equals sign. If the caller omits it, Dart quietly uses that value instead of null.

void greet({String name = "friend"}) {
  print("Hi $name");
}

Calling With a Default

Skip the parameter and the default kicks in. Pass a value and it overrides the fallback for that single call.

greet();            // Hi friend
greet(name: "Ada"); // Hi Ada

Defaults on Positional Optionals

Optional positional parameters in square brackets can carry a default too, so they never silently become null.

int pow(int base, [int exp = 2]) {
  return base * exp;
}

Defaults Must Be Constant

A default value has to be a compile-time constant. Literals and const values work, but a regular variable or function call will not.

void f({int n = 10}) {} // ok
// void g({int n = x}) {} // error

The required Keyword

Mark a named parameter with required to force callers to provide it. Forgetting it becomes a compile error, not a runtime surprise.

void send({required String to}) {}

Why required Matters

Named parameters are optional by default, so a key value could go missing. required guarantees callers always supply it. 🔒

required With Non-Null Types

A non-nullable named parameter must be either required or have a default. Otherwise Dart cannot guarantee it holds a value.

void box({required int w, int h = 10}) {}

Mixing required and Defaults

One function can blend both: some parameters required, others with sensible defaults. This gives callers a clear, flexible interface.

void user({required String id, bool active = true}) {}

Nullable Without a Default

If a named parameter is nullable, it can simply default to null. No required and no explicit default are needed.

void note({String? text}) {} // text may be null

Cleaner Call Sites

Good defaults shrink your call sites. Callers pass only what is unusual, letting the default handle the common case gracefully. ✨

Quick Check

Let's confirm you understand required and defaults.

Recap

You can now set defaults for optional parameters and use required to make key ones mandatory. Your functions are now safe and convenient. 🎉

Frequently asked questions

Is the “Default Values and required” lesson free?

Yes — the full text of “Default Values and required” 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 “Default Values and required”?

Make parameters optional or mandatory. 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 “Default Values and required” 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. Declaring Functions and Return Types
  2. Positional vs Named Parameters
  3. Default Values and required
  4. Functions as First-Class Values
← Back to Dart Academy