aleskva
aleskva

Reputation: 1805

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?

Upvotes: 19

Views: 10726

Answers (1)

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76223

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 {
/*...*/
}

Upvotes: 41

Related Questions