Lesson 20 min

Named, Optional & Default Params

00:00 / 00:00

Named, Optional & Default Parameters

Dart offers a highly flexible system for passing parameters to functions. Understanding these signatures is essential, as the Flutter framework relies heavily on named parameters for building widget layouts.


1. Positional Parameters

Positional parameters are the standard parameter style. The order in which you pass arguments must match the order in which the parameters are declared.

dart
void printProfile(String name, int age) { print("Name: $name, Age: $age"); } void main() { printProfile("Alice", 25); // Correct // printProfile(25, "Alice"); // Compile Error! }

2. Optional Positional Parameters

To make positional parameters optional, wrap them in square brackets []. Optional parameters must always be declared at the end of the parameter list. Because they are optional, their types must be nullable unless you provide a default value.

dart
// 'title' is optional and defaults to null void greet(String name, [String? title]) { if (title != null) { print("Hello, $title $name!"); } else { print("Hello, $name!"); } } // Parameter with a default value void logMessage(String msg, [String level = "INFO"]) { print("[$level]: $msg"); } void main() { greet("Alice"); // Hello, Alice! greet("Alice", "Dr."); // Hello, Dr. Alice! logMessage("System started"); // [INFO]: System started logMessage("Db connection failed", "ERROR"); // [ERROR]: Db connection failed }

3. Named Parameters

Named parameters are declared by wrapping them in curly braces {}. When calling a function with named parameters, you must specify the parameter name followed by a colon (:).

Named parameters are optional by default unless marked with the required keyword.

dart
// Named parameters signature void createUser({required String username, int status = 1, String? bio}) { print("User: $username, Status: $status, Bio: $bio"); } void main() { // Order does not matter for named parameters! createUser( bio: "Developer", username: "coder123", ); // User: coder123, Status: 1, Bio: Developer }

Mixing Parameter Types

You can mix positional and named parameters, but all positional parameters must come first, followed by the named parameter block.

dart
void downloadFile(String url, {bool showProgress = true, int retries = 3}) { print("Downloading from $url (retries: $retries)"); }

Comparison Table

Parameter TypeDeclaration SyntaxCall SyntaxDefault RequirementBest Use Case
Required Positionalfn(Type a)fn(val)YesSimple, small functions (1-2 arguments)
Optional Positionalfn([Type a])fn(val)No (Must be nullable or have default)Simple parameters that are rarely changed
Named (Optional)fn({Type a})fn(a: val)No (Must be nullable or have default)Configuration signatures, parameters with clear default values
Named (Required)fn({required Type a})fn(a: val)YesFunctions with many inputs where clarity is important

Summary

  • Positional parameters rely on order.
  • Optional positional parameters are declared inside [].
  • Named parameters are declared inside {} and must be called using their parameter names (e.g. paramName: value).
  • Use the required keyword to make a named parameter mandatory.
  • Always provide sensible default values to make your functions easy to consume.
WhatsApp