Dart Optional Parameters

In this tutorial you will learn about the Dart Optional Parameters and its application with practical example.

Dart Optional Parameters

In Dart, we are can set any parameter as optional; which allows function with optional parameter (if any) to be called/executed even if the values for those parameters is being not provided. A parameter can be set as optional by appending a question mark along with its name. Optional parameters are declared after the required parameters of a function. There are two types of optional parameter in Dart functions –

Optional Positional Parameter

Optional positional parameters can be specified by having individual parameter separated by commas and enclosed within square brackets ([]). Calling a function having optional positional parameters defined may specify a variable number of arguments.

Syntax:-

Example:-

Output:-

dart_optional_positional_parameters

Optional named parameter

Named positional parameters can be specified by having individual parameter name separated by commas and enclosed within curly brackets ({ }).

Syntax:-

Calling a function having optional named parameters, the individual parameter’s name must be specified while the value is being passed. Names are specified using parameter:value.The sequence of passing the parameter(s) does not matter.

Syntax:-

Example:-

Output:-

dart_optional_named_parameters

In this tutorial we have learn about the Dart Optional Parameters and its application with practical example. I hope you will like this tutorial.