Dart Named Parameters

Summary: in this tutorial, you’ll learn how to use the Dart named parameters to make parameters more clear in function calls.

Introduction to the Dart named parameters

The following defines a function called greet() that accepts name and title and returns a greeting message:

String greet(String name, String title) {
  return 'Hello $title $name!';
}Code language: Dart (dart)

When calling the greet() function, you need to pass the name and title arguments in the right order. In other words, you need to pass the name as the first argument and the title as the second argument:

String greet(String name, String title) {
  return 'Hello $title $name!';
}

void main() {
  print(greet('Alice', 'Professor'));
}Code language: Dart (dart)

Output:

Hello Professor Alice!Code language: Dart (dart)

If you pass the arguments in the wrong order, the function will not work properly. For example:

String greet(String name, String title) {
  return 'Hello $title $name';
}

void main() {
  print(greet('Professor', 'Alice'));
}Code language: Dart (dart)

Output:

Hello Alice Professor!Code language: Dart (dart)

The name and title are called positional parameters.

If a function uses positional parameters, when calling it, you need to provide the arguments that follow the order of the parameters.

Also, when looking at the following function call, you may not understand it fully:

greet('Alice', 'Professor')Code language: Dart (dart)

To resolve this, you can use named parameters. Named parameters make the meaning of parameters clear in the function calls.

To define named parameters, you surround them with curly braces. For example:

String greet(String name, {String title = ''}) {
  if (title.isEmpty) {
    return 'Hello $name!';
  }
  return 'Hello $title $name!';
}Code language: Dart (dart)

In this example, we change the title from a positional parameter to a named parameter. The named parameter is also optional. Therefore, you need to assign a default value to it.

Also, when calling the greet() function, you need to specify the parameter name like this:

String greet(String name, {String title = ''}) {
  if (title.isEmpty) {
    return 'Hello $name!';
  }
  return 'Hello $title $name!';
}

void main() {
  print(greet('Alice', title: 'Professor'));
}Code language: Dart (dart)

This example calls the greet() function and specifies the named parameter:

greet('Alice', title: 'Professor')Code language: Dart (dart)

A function can have either optional parameters or named parameters, but not both.

Making named parameters required

The named parameters are optional by default. It means that you need to specify the default values for the named parameters when defining the function. For example:

void connect(String host,
    {int port = 3306, String user = 'root', String password = ''}) {
  print('Connecting to $host on $port using $user/$password...');
}

void main() {
  connect('localhost');
}Code language: Dart (dart)

Output:

Connecting to localhost on 3306 using root/...Code language: Dart (dart)

To make a named parameter required, you add the required keyword in front and remove the default value.

The following example makes the user and password parameters required:

void connect(String host,
    {int port = 3306, required String user, required String password}) {
  print('Connecting to $host on $port using $user/$password...');
}

void main() {
  connect('localhost', user: 'root', password: 'secret');
}Code language: Dart (dart)

Summary

  • Use Dart named parameters to make the parameters clear in function calls.
  • Use {} to surround the named parameters.
  • By default, named parameters are optional. Use the required keyword to make them required.
  • Specify the parameter names when calling a function with named parameters.
Was this tutorial helpful ?