Skip to main content

Flutter & Dart: Optional Named Class parameters

In flutter it is common to see a format like 

Curly brackets {} are used to specify optional, named parameters in Dart.

someWidget(

title: "Title of my Widget"
backgroundColor: Colors.red

}

These are simply the named parameters the class accepts

someWidget( title: "Title of my Widget" backgroundColor: Colors.red}

That come from the properties of the someWidget class.

These are optional named parameters of the Flutter class.

Dart has two types of optional parameters: named and positional

 

A parameter wrapped by { } is a named optional parameter. Here is an example:

getHttpUrl(String server, String path, {int port = 80}) {
  // ...
}

You can call getHttpUrl with or without the third parameter. You must use the parameter name when calling the function.

 

Dart has two types of optional parameters: named and positional. Before I discuss the differences, let me first discuss the similarities.

Dart's optional parameters are optional in that the caller isn't required to specify a value for the parameter when calling the function.

Optional parameters can only be declared after any required parameters.

Optional parameters can have a default value, which is used when a caller does not specify a value.

Positional optional parameters

A parameter wrapped by [ ] is a positional optional parameter. Here is an example:

getHttpUrl(String server, String path, [int port=80]) {
  // ...
}

In the above code, port is optional and has a default value of 80.

You can call getHttpUrl with or without the third parameter.

getHttpUrl('example.com', '/index.html', 8080); // port == 8080
getHttpUrl('example.com', '/index.html');       // port == 80

You can specify multiple positional parameters for a function:

getHttpUrl(String server, String path, [int port=80, int numRetries=3]) {
  // ...
}

The optional parameters are positional in that you can't omit port if you want to specify numRetries.

getHttpUrl('example.com', '/index.html');
getHttpUrl('example.com', '/index.html', 8080);
getHttpUrl('example.com', '/index.html', 8080, 5);

Of course, unless you know what 8080 and 5 are, it's hard to tell what those apparently magic numbers are. You can use named optional parameters to create more readable APIs.

Named optional parameters

A parameter wrapped by { } is a named optional parameter. Here is an example:

getHttpUrl(String server, String path, {int port = 80}) {
  // ...
}

You can call getHttpUrl with or without the third parameter. You must use the parameter name when calling the function.

getHttpUrl('example.com', '/index.html', port: 8080); // port == 8080
getHttpUrl('example.com', '/index.html');             // port == 80

You can specify multiple named parameters for a function:

getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) {
  // ...
}

Because named parameters are referenced by name, they can be used in an order different from their declaration.

 

Required Parameter

 

 

 Optional Positional Parameter

parameter will be disclosed with square bracket [ ] & square bracketed parameter are optional.

 

Optional Named Parameter

  • parameter will be disclosed with curly bracket { }
  • in curly bracketed parameter order does not matter
  • these type parameter help us to avoid confusion while passing value for a function which has many parameter