Optional Arguments

One of the more tedious chores in most languages is extracting optional arguments. Dart’s take on the matter is an altogether pleasant built-in syntax.

Let’s say that we want a function named good_day to print a pleasant greeting, with an optional flag to indicate a follow-up message. Calling such a function in Dart looks like the following.

functional_programming/optional.dart
 
good_day(​"Bob"​);
 
// Good day, Bob.
 
good_day(​"Bob"​, emphatic: true);
 
// Good day, Bob.
 
// I said good day!

In the second call, we supply the optional emphatic: true option to get the extra message. At first glance, this might look like a HashMap. Closer inspection reveals that there are no curly braces around it. This is not a HashMap

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.