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:
nameof typeStringandmessageof typeString(optional positional).Line 2: Inside the function an if statement is used to check if the
messageparameter is notnull.Line 3: If the
messageis notnull, it means a value was provided when calling the function, so it prints the concatenated string ofmessage, followed by a comma and thename.Line 5: If the
messageisnull, which means no value was provided formessage, so it prints a generic greeting of "Hello" followed by thename.Line 10: Only the
nameargument is provided in the first function callfunction('Alice'), and themessageargument is omitted. Therefore, the function printsHello, Alice!as the default greeting.Line 11: In the second function call
function('Bob', 'Good morning'),nameandmessagearguments are provided. The function recognizes the provided message value asGood morningand printsGood 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
nameargument is provided using the named parameter syntax in the first function callfunction(name: 'Alice'). Themessageargument is omitted. Therefore, the function printsHello, Alice!as the default greeting.Line 11: In the second function call
function(message: 'Good evening', name: 'Bob'), bothnameandmessagearguments are provided using the named parameter syntax. The function recognizes the provided values formessageandnameand printsGood 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:
nameandmessage, both of typeStringin curly brackets{}having the default values assigned to them.Line 6: No arguments are provided in the first function call
function(), sonameandmessageparameters use their respective default values. Therefore, the function prints "Hello, Guest!" as the default greeting.Line 7: Only the
nameargument is explicitly provided using the named parameter syntax in the second function callfunction(name: 'Alice'). The message argument is omitted, so it uses its default value. The function printsHello, Alice!as the greeting.Line 8: In the third function call
function(message: 'Good afternoon', name: 'Bob'),nameandmessagearguments are explicitly provided using the named parameter syntax. The function recognizes the provided values formessageandname, and printsGood 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.
Free Resources