Difference Between Named and Positional Parameters In Dart?

· 6 min read
What is the Difference Between Named and Positional Parameters in Dart
What is the Difference Between Named and Positional Parameters in Dart

Dart supports both named optional parameters and positional optional parameters. So in this article, we will go through What is the Difference Between Named Parameter and Positional Parameters In Dart.

What is the Difference Between Named and Positional Parameters in Dart?

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. So Optional parameters can have a default value, that 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, the port is optional and has a default value of 80.

So for this, 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]) {
  // ...
}

So 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}) {
  // ...
}

So 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

For you can specify multiple named parameters for a function:

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

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

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

We believe named parameters make for easier-to-understand call sites, especially when there are boolean flags or out-of-context numbers.

So in Dart with my understanding, the method parameter can be given in two types.

  • Required parameter
  • Optional parameter (positional, named & default)
Required Parameter: Required parameter is a well know old-style parameter that we all familiar with it.

Example:

findVolume(int length, int breath, int height) {
 print('length = $length, breath = $breath, height = $height');
}

findVolume(10,20,30);

Output:

length = 10, breath = 20, height = 30

Optional Positional Parameter: parameter will be disclosed with the square bracket [ ] & square bracketed parameter are optional.

Example:

findVolume(int length, int breath, [int height]) {
 print('length = $length, breath = $breath, height = $height');
}

findVolume(10,20,30);//valid
findVolume(10,20);//also valid

Output:

length = 10, breath = 20, height = 30
length = 10, breath = 20, height = null // no value passed so height is null
  • Optional Named Parameter
    • The curly bracket will dispose the parameter.
    • curly bracketed parameters are optional.
    • have to use parameter name to assign a value which separated with colan :
    •  in curly bracketed parameter order does not matter
    • these type parameters help us to avoid confusion while passing value for a function which has many parameters.

So here is an Example:

findVolume(int length, int breath, {int height}) {
 print('length = $length, breath = $breath, height = $height');
}

findVolume(10,20,height:30);//valid & we can see the parameter name is mentioned here.
findVolume(10,20);//also valid

Output:

length = 10, breath = 20, height = 30
length = 10, breath = 20, height = null
  • Optional Default Parameter
    • same like optional named parameter in addition we can assign a default value for this parameter.
    • So it will take no value passed as default.
  • Example:
    findVolume(int length, int breath, {int height=10}) {
     print('length = $length, breath = $breath, height = $height');
    } 
    
    findVolume(10,20,height:30);//valid
    findVolume(10,20);//valid
  • Output:
    length = 10, breath = 20, height = 30
    length = 10, breath = 20, height = 10 // default value 10 is taken

Conclusion:

Thanks for reading !!!

So in this article, we have been through what is the difference between named and positional parameters in Dart.

Keep Learning !!! Keep Fluttering !!!

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply