Named Parameters and Positional Parameters in Dart Functions

In Dart, functions can accept parameters in two main ways: positional parameters and named parameters. Understanding these two types of parameters is essential for writing flexible and readable functions.

1. Positional Parameters

Positional parameters are the most common type of parameters. They are defined in the order they are expected to be passed to the function. When calling a function with positional parameters, you must provide the arguments in the same order as the parameters are defined.

Example of Positional Parameters

void printInfo(String name, int age) {
print('Name: $name');
print('Age: $age');
}

void main() {
printInfo('Alice', 30); // Correct usage
// printInfo(30, 'Alice'); // This would cause an error due to incorrect order
}

In this example, the printInfo function takes two positional parameters: name and age. When calling the function, the arguments must be provided in the same order.

2. Named Parameters

Named parameters allow you to specify the names of the parameters when calling a function. This makes the function calls more readable and allows you to pass arguments in any order. Named parameters can be defined as optional and are enclosed in curly braces {}.

Example of Named Parameters

void printInfo({String name, int age}) {
print('Name: $name');
print('Age: $age');
}

void main() {
printInfo(name: 'Alice', age: 30); // Correct usage with named parameters
printInfo(age: 25, name: 'Bob'); // Order does not matter
}

In this example, the printInfo function uses named parameters. When calling the function, you can specify the names of the parameters, allowing you to pass them in any order.

3. Required Named Parameters

By default, named parameters are optional. However, you can make them required by using the @required annotation or by using the required keyword (available in Dart 2.12 and later).

Example of Required Named Parameters

void printInfo({required String name, required int age}) {
print('Name: $name');
print('Age: $age');
}

void main() {
printInfo(name: 'Alice', age: 30); // Correct usage
// printInfo(name: 'Bob'); // This would cause an error because age is required
}

4. Mixing Positional and Named Parameters

You can also mix positional and named parameters in a function. However, positional parameters must come before named parameters in the function definition.

Example of Mixing Parameters

void printInfo(String name, {int age, String city}) {
print('Name: $name');
if (age != null) {
print('Age: $age');
}
if (city != null) {
print('City: $city');
}
}

void main() {
printInfo('Alice', age: 30, city: 'New York'); // Correct usage
}

Conclusion

Named parameters and positional parameters in Dart functions provide flexibility in how you define and call functions. Positional parameters require arguments to be passed in a specific order, while named parameters allow for more readable function calls and can be passed in any order. Understanding how to use both types effectively will help you write cleaner and more maintainable Dart code.