dcli
  • Introduction
  • What does DCli do?
  • Install DCli
    • Installing on Windows
  • Writing your first CLI app
  • Add DCli to your project
  • pub.dev
  • github
  • Dart basics
    • Dart lambda functions
    • Function Arguments
    • Futures
    • stdin/stdout/stderr a primer
  • Tour
    • Overview
    • Using DCli functions
    • User input
      • Ask Validators
    • Displaying information
    • Managing Files And Directories
    • Environment variables
    • Calling apps
    • Redirecting output
    • Command Line Arguments
    • Paths
    • Glob Expansion
    • Piping
    • Locking
    • Fetch
    • The evils of CD
    • Assets/Resources
    • Cross Platform
      • Posix
      • Windows
      • Docker
        • Detecting Docker
        • Add DCli to a Docker Container
        • Example DCli app in Docker
  • Elevated Privileges
    • Sudo
  • Performance
  • Dependency Management
    • Dependency Management
    • Pubspec Managment
  • DCli Tools
    • DCli tools
    • Use a shebang #!
    • DCli Compile
    • DCli Clean
    • DCli Create
    • DCli Doctor
    • DCli Install
    • DCli Run
    • DCli Warmup
    • DCli Pack
    • Upgrade DCli
  • Internal Workings
    • Internal Workings
    • waitForEx
  • Contributing
    • Creating a release
    • Running Unit tests
    • Implemention support for a shell
    • Templates
  • References
  • Examples
    • Projects
    • Code
      • hello world.
      • dcompress
      • dpath
      • dmysql
      • dshell
      • dwhich
      • dipaddr
      • gnome launcher
  • Articles
    • build CLI apps in dart - part 1
    • build CLI apps in dart - part 2
    • Dealing with permissions
    • 3rd Party console packages
  • Dart on Linux - the perfect CLI tooling
  • Improving your build environment
    • Existing tooling
    • Building with Dart
    • A home for your build tools
  • Olivier Revial - CLI apps made easy
  • Video: package of the week
Powered by GitBook
On this page

Was this helpful?

  1. Dart basics

Function Arguments

PreviousDart lambda functionsNextFutures

Last updated 1 year ago

Was this helpful?

This section provides details on the Dart language:

To learn more about Dart's syntax read the Dart language tour.

Dart allows three types of arguments to be passed to a method or function.

Positional, optional and named.

Positional arguments are traditional 'C' style arguments that everyone is familiar with.

Optional arguments are positional arguments that are (you guessed it) optional.

Named arguments are a little trickier but depending on your current language experience you may already be familiar with them.

Named arguments allow you to pass an argument by name rather than position and they are (usually) optional.

To declare a named argument you use curly braces {}.

void testMethod(String arg1, [String arg2], {String? arg3, String? arg4});

In the above example arg1 is a positional argument, arg2 is an optional argument with arg3 and arg4 being optional named arguments.

To call the test method passing values to all of the above arguments you would use:

testMethod('value1', 'value2' , arg4: 'value4', arg3: 'value3');

Note that I've reversed the order of arg3 and arg4. As they are named arguments you can place them in any position AFTER the named and optional arguments.

Let's finish with an example using the forEach method:

'tail /var/log/syslog'.forEach((line) => print(line), stderr:(line) => print(line));

The above example will print any output sent to stdout or stderr from the 'tail' command.

https://dart.dev/guides/language/language-tour