Why dart optional named argument is not null if not provided?

330 views Asked by At

When optional named parameter is not provided, why it is not null as expected?

void main() {
  num double({v: num}) {
    if (v == null)
      return 0;
    else
      return v * 2;
  }

  print(double(v: 2));
  print(double());
  print('done');
}

which output as

4
Uncaught TypeError: v.$mul is not a function
1

There are 1 answers

0
Günter Zöchbauer On BEST ANSWER
num double({v: num}) {

defines a named parameter v of type dynamic with the default value num (a type)

It should instead be

num double({num v}) {

to make your code work as expected