What are Dart optional parameters?

Dart optional parameters are parameters that are not required when calling a function, allowing flexibility in providing values. This means that the caller of the function can choose to pass a value for the optional parameter or omit the parameter altogether.

Dart has three types of optional parameters.

  • Optional positional parameters

  • Optional named parameters

  • Optional parameters with default values

Optional positional parameters

Positional optional parameters are declared inside square brackets [] and are identified by their position in the argument list. When calling the function, these parameters can be omitted.

Syntax

void functionName(parameter1 ,[parameter2, parameter3, ...]) {
// Function body
}

In the case of optional positional parameters, if a value is not passed, then it will be assigned a default value of null

Code example

void function(String name, [String message]) {
if (message != null) {
print('$message, $name!');
} else {
print('Hello, $name!');
}
}
void main() {
function('Alice'); // Hello, Alice!
function('Bob', 'Good morning'); // Good morning, Bob!
}

Explanation

  • Line 1: The function is defined with two parameters: name of type String and message of type String (optional positional).

  • Line 2: Inside the function an if statement is used to check if the message parameter is not null.

  • Line 3: If the message is not null, it means a value was provided when calling the function, so it prints the concatenated string of message, followed by a comma and the name.

  • Line 5: If the message is null, which means no value was provided for message, so it prints a generic greeting of "Hello" followed by the name.

  • Line 10: Only the name argument is provided in the first function call function('Alice'), and the message argument is omitted. Therefore, the function prints Hello, Alice! as the default greeting.

  • Line 11: In the second function call function('Bob', 'Good morning'), name and message arguments are provided. The function recognizes the provided message value as Good morning and prints Good morning, Bob!.

Optional named parameters

Optional named parameters are enclosed in curly braces {} and can be identified by their names when calling the function. These parameters provide more flexibility by allowing you to specify only the desired parameters, regardless of their order.

Syntax

void functionName(parameter1 ,{parameter2, parameter3, ...}) {
// Function body
}

Code example

void function({String name, String message}) {
if (message != null) {
print('$message, $name!');
} else {
print('Hello, $name!');
}
}
void main() {
function(name: 'Alice'); // Hello, Alice!
function(message: 'Good evening', name: 'Bob'); // Good evening, Bob!
}

Explanation

  • Line 1–7: The function is doing the same thing as the previous function, but this time it is called with named parameters in curly brackets {}.

  • Line 10: Only the name argument is provided using the named parameter syntax in the first function call function(name: 'Alice'). The message argument is omitted. Therefore, the function prints Hello, Alice! as the default greeting.

  • Line 11: In the second function call function(message: 'Good evening', name: 'Bob'), both name and message arguments are provided using the named parameter syntax. The function recognizes the provided values for message and name and prints Good evening, Bob!.

Optional parameters with default values

Default optional parameters allow you to provide a default value when the parameter is not explicitly passed in the function call. It ensures that the function works even if specific parameters are not provided.Conclusion

Syntax

void functionName(parameter1 ,{parameter2 = defaultValue2, parameter3 = defaultValue3, ...}) {
// Function body
}

Code example

void function({String name = 'Guest', String message = 'Hello'}) {
print('$message, $name!');
}
void main() {
function(); // Hello, Guest!
function(name: 'Alice'); // Hello, Alice!
function(message: 'Good afternoon', name: 'Bob'); // Good afternoon, Bob!
}

Explanation

  • Lines 1–3: The function is defined with named parameters: name and message, both of type String in curly brackets {} having the default values assigned to them.

  • Line 6: No arguments are provided in the first function call function(), so name and message parameters use their respective default values. Therefore, the function prints "Hello, Guest!" as the default greeting.

  • Line 7: Only the name argument is explicitly provided using the named parameter syntax in the second function call function(name: 'Alice'). The message argument is omitted, so it uses its default value. The function prints Hello, Alice! as the greeting.

  • Line 8: In the third function call function(message: 'Good afternoon', name: 'Bob'), name and message arguments are explicitly provided using the named parameter syntax. The function recognizes the provided values for message and name, and prints Good afternoon, Bob!.

Conclusion

Dart's optional parameters, including positional, named, and default, offer developers great flexibility in defining and calling functions. We can write more concise and versatile code in Dart by effectively understanding and utilizing optional parameters.

Copyright ©2024 Educative, Inc. All rights reserved