Skip to content
Blog

Setting Defaults and Marking Parameters as Required in Dart

How Dart parameter defaults must be compile-time constants, why optional params are nullable, and when to use required named parameters instead of defaults.

Published on • December 3, 2026

AI Assistant

Defaults and required are how a function decides what happens when the caller says less than you expected — and Dart’s null-safety rules make getting this combination right essential to every API you write. Get it wrong and you either ship surprising nulls or force callers to write boilerplate they shouldn’t have to.

Default values must be compile-time constants

The single hard rule: a default value is evaluated at compile time, so it must be a constant expression — literals, const constructors, and top-level const variables only.

String say(String from, String msg, [String device = 'carrier pigeon']) {
  return '$from says $msg with a $device';
}

say('Bob', 'Howdy'); // 'Bob says Howdy with a carrier pigeon'

The classic mistake is a non-const collection literal. This does not compile:

// Error: isn't a valid default value
void addAll(List<int> values, [List<int> extra = []]) {}

The fix is const [] (or const <int>[] for an explicit type):

void addAll(List<int> values, [List<int> extra = const []]) {}

The same applies to named parameters: {int count = 2} is fine because 2 is constant; {List<int> tags = const []} is required for lists.

Optional parameters without defaults default to null

Under null safety, if an optional parameter has no default value, its implicit default is null — so its type must be nullable. Effective Dart adds a style rule on top: DON’T write an explicit default of null.

// Bad — redundant
void error([String? message = null]) {}

// Good — null is the implicit default
void error([String? message]) {
  stderr.write(message ?? '\n');
}

This is the null-safety interplay in one snippet: optional + no default ⇒ nullable type + implicit null. If you want a non-nullable parameter with a real fallback, you must supply a constant default. The compiler enforces all of this statically — declaring void f([int n]) where int is non-nullable is a compile error, not a warning you can ignore.

required makes a named parameter mandatory

Named parameters are all optional unless annotated with required. Once marked, the analyzer enforces it — omitting the argument is a compile-time error, not a runtime surprise:

const Scrollbar({super.key, required Widget child});

Try to build Scrollbar() without child: and the analyzer reports an issue immediately. Unlike a runtime ArgumentError, this failure happens while you type — your IDE underlines the constructor call, and the build never starts. That is the real appeal of required: it converts a whole class of “forgot the argument” bugs into editor diagnostics. Effective Dart’s guidance on sentinel values connects here: AVOID mandatory parameters that accept a special “no argument” value. If a caller is logically omitting the argument, make it optional rather than forcing them to pass null or an empty string:

// Bad — caller must pass a magic "nothing" value
var rest = string.substring(start, null);

// Good — caller simply omits it
var rest = string.substring(start);

required-but-nullable is still allowed

required and nullability are independent axes. You can require a caller to mention a parameter while still allowing them to pass null:

const Scrollbar({super.key, required Widget? child});

Here child: must always be supplied, but child: null is legal. This is useful when “explicitly absent” differs from “forgot to say.” In practice, though, a non-nullable required Widget child is the stronger contract — prefer it unless null carries real meaning. And remember the two markers compose with defaults in only one sensible way: a parameter can have a default or be required, never both, because a value the function already supplies is never something the caller must provide.

Defaults calling const constructors

Because defaults must be constant, they work beautifully with const constructors — and that’s exactly why Effective Dart says CONSIDER making your constructor const if the class supports it. A const constructor unlocks use inside default values, annotations, and other constant contexts:

class Padding {
  final double all;
  const Padding(this.all);
}

// Now callers can default to it:
void frame([Padding inset = const Padding(8)]) {}

If the constructor weren’t const, that default would be a compile error. Effective Dart also warns that making it const is a public API commitment — you can’t later downgrade it without breaking users.

Defaults versus null checks: a pattern comparison

ApproachSignatureUse when
Implicit nullf([int? width])Absence is meaningful; caller may pass null
Constant defaultf([int width = 800])There’s always a sensible fallback
Required namedf({required int width})Caller must always decide; no good default
Sentinel parameterf(int width = -1)Avoid — magic values hide bugs

A common real-world pattern combines default and explicit handling: give the parameter a constant default when possible, and use ?? or a null check only when the value is genuinely nullable. Prefer the default over a manual if (x == null) x = 800; check inside the body — the fallback becomes visible in the signature, where callers and documentation generators can see it. A nullable subtitle handled with ?? looks like this:

String banner(String title, [String? subtitle]) {
  final sub = subtitle ?? 'No subtitle';
  return '$title — $sub';
}

A real Flutter example

Flutter’s Scrollbar constructor ties the whole lesson together: a named constructor where the widget’s essential part is required, while styling knobs stay optional with defaults:

const Scrollbar({
  super.key,
  required Widget child,
  bool interactive = true,
  double thickness = 8.0,
});

Callers must provide child: — the analyzer enforces it — but interactive and thickness fall back to constants when omitted. The const on the constructor is what lets those defaults (and the widget itself) live in constant contexts throughout a widget tree.

Takeaways

Optional parameters default to null and must therefore be nullable unless you give them a compile-time constant default. Use required when the caller must always decide, keep required parameters non-nullable unless null is truly meaningful, and never smuggle “no argument” through a magic sentinel value.

Takeaway: Give optional parameters constant defaults, use required when the caller must always decide, and avoid sentinel values — defaults, required, and nullability should each do one clear job.

References