Ad space (banner)
Lesson 14 / 20

Named arguments

This lesson covers named arguments, which let you specify an argument by name.

Wrapping a parameter in {}, as in {type name}, lets the caller pass it as name: value. You can then pass only the arguments you need, in any order.

The sample code defines void greet(String name, {String title = 'Mr./Ms.'}) and calls it both with the title left out and with it supplied.

A common early stumble is the difference in syntax between ordinary (positional) arguments and named ones. A parameter wrapped in {} has to be passed with name: at the call site.

In professional work, almost every Flutter widget constructor is built from named arguments, which makes it obvious at the call site what each value means.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)