Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values of an optional parameter

I have a future and I want first optional parameter of that future to be an empty list. But dartanalyzer myFile.dart returns this error:

[error] Default values of an optional parameter must be constant
(/home/user/projects/project/lib/myFolder/myFile.dart, line 7, col 48)

My code:

Future<dynamic> myFuture([List<Node> content = []]) async {
/*...*/
}

How can I get rid of this error?

like image 741
aleskva Avatar asked Jul 24 '15 13:07

aleskva


People also ask

Which parameters are default values?

A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and "Norway" is the default value.

Can parameters have default values?

In JavaScript, a parameter has a default value of undefined. It means that if you don't pass the arguments into the function, its parameters will have the default values of undefined .

How do I give a default value to optional parameter TypeScript?

Use default parameter syntax parameter:=defaultValue if you want to set the default initialized value for the parameter. Default parameters are optional. To use the default initialized value of a parameter, you omit the argument when calling the function or pass the undefined into the function.

What is optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters.


1 Answers

You need to use a constant as default parameter. To define a constant list you need to use the prepending const keyword:

Future<dynamic> myFuture([List<Node> content = const []]) async {
/*...*/
}
like image 60
Alexandre Ardhuin Avatar answered Oct 09 '22 11:10

Alexandre Ardhuin