Flutter/Dart - Optional (Name and Positional) Parameters - 2023

In flutter development or any programming language, We can't imagine without function. We know function calls with parameters or without parameters however if we need to pass a parameter in the function we can use named or positional parameters as an optional parameters. So in this article, I am going to share with you Optional (Name and Positional) Parameters.

What is the use of optional parameters:

In flutter, we can use types of optional parameters named and positional. Optional parameters mean optional in that the caller isn't required to specify a value for the parameter when calling the function.

Rules:

We can declare optional parameters 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 positional parameter is wrapped by [ ]. You can see the below example.

Without the default value of  the third parameter : 

getBasicInfo(String name, String lastName, [String? dob]) {
    print("Name--->$name");
    print("Last Name--->$lastName");
    print("DOB--->$dob");
  }

 

getBasicInfo("Virat","Kohali");
---------------------------------
Output
I/flutter (14438): Name--->Virat
I/flutter (14438): Last Name--->Kohali
I/flutter (14438): DOB--->null

 

With the default value of  the third parameter : 

getBasicInfo(String name, String lastName, [String? dob = "18-11-1998"]) {
    print("Name--->$name");
    print("Last Name--->$lastName");
    print("DOB--->$dob");
  }

 

getBasicInfo("Virat","Kohali");
-----------------------------------------
Output
I/flutter (14438): Name--->Virat
I/flutter (14438): Last Name--->Kohali
I/flutter (14438): DOB--->18-11-1998


getBasicInfo("Virat","Kohali","1-01-1990");
----------------------------------------------------
I/flutter (14438): Name--->Virat
I/flutter (14438): Last Name--->Kohali
I/flutter (14438): DOB--->1-01-1990

 

Named optional parameters:

- A Named positional parameter is wrapped by { }.

- You can see the below example. We can call the function with or without the parameter. We must use the parameter name when calling the function.

- Name optional parameters to make code more readable especially boolean flags or out-of-context numbers.

Without the default value of  the third parameter : 

getBasicInfo(String name, String lastName, {String? dob}) {
    print("Name--->$name");
    print("Last Name--->$lastName");
    print("DOB--->$dob");
  }

 

getBasicInfo("Virat","Kohali");
-----------------------------------------
I/flutter (14438): Name--->Virat
I/flutter (14438): Last Name--->Kohali
I/flutter (14438): DOB--->null

 

With the default value of  the third parameter :

getBasicInfo(String name, String lastName, {String? dob="18-11-1988"}) {
    print("Name--->$name");
    print("Last Name--->$lastName");
    print("DOB--->$dob");
  }

 

getBasicInfo("Virat","Kohali");
-----------------------------------------
I/flutter (14438): Name--->Virat
I/flutter (14438): Last Name--->Kohali
I/flutter (14438): DOB--->18-11-1988


getBasicInfo("Virat","Kohali",dob: "20-5-1998");
-----------------------------------------
I/flutter (14438): Name--->Virat
I/flutter (14438): Last Name--->Kohali
I/flutter (14438): DOB--->20-5-1998

Note:

We can use positional optional parameters or named optional parameters, but not both in the same function or method. The following is not allowed.

getBasicInfo(String name, String lastName, [String? age="25"],{String? dob="18-11-1988"}) {
    print("Name--->$name");
    print("Last Name--->$lastName");
    print("DOB--->$dob");
  }

 

Conclusion:

We saw Optional (Name and Positional) Parameters step by step with examples so I hope this clears all doubt regarding the optional parameter. I hope this article may help you.

Tags: