Dart - parameters in functions (english)

At the moment I'm posting a little series about Flutter on this blog (the last one here). In this series, I'll get to the point where the majority will soon be about code, dart code.

As a beginner, it's very easy to forget that Flutter Code is Dart Code!

One reason, not the only one, is the spelling. This post addresses the issue.





Positional parameters

A simple function as an example:

Example one
This function has no return value and first expects a character string as a parameter, then an integer value. You could try calling the function like this:

Example two

The code above won't work! The print function expects the character string first and then the number! That is why we speak of positional parameters!

You can also set optional parameters in the print function. Optional parameters are defined in [...]. They can be passed when the function is called, but do not have to be. Example:

Example three
In the code above I don't use the optional parameter, the code still executes without errors. Logically, what I didn't use was optional!

Named parameters

The variant that often come across in Flutter! Optional, named parameters are defined in {...} brackets. Example:

Example five
In the example code, I also assign default values to the two optional, named parameters. nAme is printed out if it does not have the content dummy. You could now adapt the function so that it is also uses nachName !

The complete code of the example:

Example six
It is also important: you cannot write drucken(nAme: 'wonderful', 2)! Dart requires the positional parameter first, i.e. int alter, and only then the optional parameters.

Finally, another example without any positional parameters:

Example seven
As you can see in the example above, the order in which you pass the optional, named parameters does not matter! See you soon !


Kommentare

Beliebte Posts aus diesem Blog

Dart Final Const

Flutter -- ohne Dart geht es nicht 2 -- einfache Variablen Typen

Flutter BloC Pattern 1