Named and positional parameters in Dart

Named and positional parameters in Dart

In Dart, we have two types of optional parameters: named and positional.

  • 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.

Optional parameters are parameters which don’t have to be specified when calling given function. Optional parameters must be declared after required parameters. Additionally, optional parameters can have a default value, which is used once the function invocation doesn’t specify it.

👉Dart Cheat Sheet and Quick Reference

Positional Parameters in Dart

Square brackets [] are used to specify optional, positional parameters in Dart.

void hamViDu(int a, [int b = 0, int c = 0]) {
  if (b != 0 && c != 0) {
    print('a:$a - b:$b - c:$c');
  } else if (b != 0) {
    print('a:$a - b:$b');
  } else {
    print('a:$a');
  }
}

void main() {
  hamViDu(5, 10, 15);

  print('----------------------');
  hamViDu(5);

  print('----------------------');
  hamViDu(5, 10);
}

For such declaration a is always required while b and c are optional. Also, b, c will default to 0 if not specified.

hamViDu(5, 10, 15); //print a:5 - b:10 - c:15

hamViDu(5); //print a:5

hamViDu(5, 10);//print a:5 - b:10

Additionally, the parameter names are specified and visible only at the level of the function declaration. The function caller must know which position corresponds to which parameter. This leads to a slightly less readable code.

Named parameters in Dart

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

readFile(String name, {String mode, String charset = 'utf-8'}) { 
//......
}

Named parameters are referenced by name, which means that they can be used during the function invocation in an order different from the function declaration.

void hamViDu({int a = 0, int b = 0, int c = 0}) {
  print('a:$a - b:$b - c:$c');
}

void main() {
  hamViDu();

  print('----------------------');
  hamViDu(a: 5, c: 15, b: 10);

  print('----------------------');
  hamViDu(c: 5);
  
  print('----------------------');
  hamViDu(b: 5, c: 10);
}

Checking if optional parameter was provided

Unfortunately, you cannot distinguish between the cases “an optional parameter was not provided” and “an optional parameter was provided with the default value”.

Note: You may use positional optional parameters or named optional parameters, but not both in the same function or method. The following is not allowed.

thisFunctionWontWork(String foo, [String positonal], {String named}) {
 // will not work! 
}
Các bài viết, tài liệu được chúng tôi sưu tầm và chia sẻ thường không rõ tác giả. Nếu bạn thấy ảnh hưởng quyền lợi, vi phạm bản quyền xin gửi mail tới phuong@dayhoc.page. Xin cám ơn!
Leave a Reply
Previous Article

Các loại backlink hỗ trợ SEO

Next Article

Mẫu giáo án Tự nhiên và xã hội lớp 2 sách Kết nối tri thức với cuộc sống

Related Posts