Dart Named Parameters

Making function calls clear and readable with named arguments

🏷️ What are Named Parameters?

Named parameters in Dart allow you to specify arguments by name when calling functions. This makes code more readable and lets you pass arguments in any order.


// Function with named parameters
void createUser({required String name, int age = 18, String role = 'user'}) {
  print('Creating user: $name, Age: $age, Role: $role');
}

// Call with named arguments
createUser(name: 'Alice', role: 'admin', age: 25);
                                    

Output:

Creating user: Alice, Age: 25, Role: admin

Named Parameter Features

Required Named

Must be provided when calling

void login({required String email}) {
  print('Logging in: $email');
}
🔧

Optional Named

Can be omitted with defaults

void setup({String theme = 'light'}) {
  print('Theme: $theme');
}
🔄

Any Order

Arguments can be in any sequence

draw(color: 'red', size: 10, x: 5, y: 3);
draw(y: 3, color: 'blue', x: 5, size: 8);
📖

Self-Documenting

Code explains itself clearly

sendEmail(
  to: '[email protected]',
  subject: 'Welcome!'
);

🔹 Basic Named Parameters

Define named parameters using curly braces {}:

// Function with named parameters
void printInfo({String? name, int? age, String? city}) {
  print('--- User Info ---');
  if (name != null) print('Name: $name');
  if (age != null) print('Age: $age');
  if (city != null) print('City: $city');
}

void main() {
  printInfo(name: 'Alice');
  printInfo(age: 30, city: 'Boston');
  printInfo(name: 'Bob', age: 25, city: 'Seattle');
}

Output:

--- User Info ---
Name: Alice

--- User Info ---
Age: 30
City: Boston

--- User Info ---
Name: Bob
Age: 25
City: Seattle

🔹 Required Named Parameters

Use the 'required' keyword to make named parameters mandatory:

// Function with required named parameters
void connectDatabase({
  required String host,
  required int port,
  String username = 'admin',
  String? password
}) {
  print('Connecting to $host:$port');
  print('Username: $username');
  if (password != null) print('Password: ${'*' * password.length}');
}

void main() {
  connectDatabase(
    host: 'localhost',
    port: 5432,
    password: 'secret123'
  );
}

Output:

Connecting to localhost:5432

Username: admin

Password: *********

🔹 Mixed Parameters

Combine positional and named parameters:

// Function with both positional and named parameters
String formatText(String text, {
  bool bold = false,
  bool italic = false,
  String color = 'black'
}) {
  String result = text;
  if (bold) result = '**$result**';
  if (italic) result = '*$result*';
  return '[$color] $result';
}

void main() {
  print(formatText('Hello World'));
  print(formatText('Important!', bold: true, color: 'red'));
  print(formatText('Emphasis', italic: true, bold: true));
}

Output:

[black] Hello World

[red] **Important!**

[black] ***Emphasis***

🔹 Real-World Example

Creating a configuration function with named parameters:

// App configuration with named parameters
void configureApp({
  required String appName,
  String version = '1.0.0',
  bool debugMode = false,
  int maxUsers = 100,
  List features = const ['basic']
}) {
  print('=== App Configuration ===');
  print('App: $appName v$version');
  print('Debug Mode: ${debugMode ? 'ON' : 'OFF'}');
  print('Max Users: $maxUsers');
  print('Features: ${features.join(', ')}');
}

void main() {
  configureApp(
    appName: 'MyApp',
    debugMode: true,
    features: ['auth', 'chat', 'notifications']
  );
}

Output:

=== App Configuration ===

App: MyApp v1.0.0

Debug Mode: ON

Max Users: 100

Features: auth, chat, notifications

🧠 Test Your Knowledge

How do you make a named parameter required in Dart?