Dart Parameters

⋅ 5 min read ⋅ Flutter Dart

Table of Contents

Dart has three ways to declare parameters for a function.

  1. Required positional parameters.
  2. Named parameters.
  3. Optional positional parameters.

You can use multiple types of parameters in the same function, but there is a strict rule on using them.

First, let's learn each type of parameter one by one.

Then after you know the difference between the three, we will come back to learn the rule in the How to declare parameters in Dart section.

Dart required positional parameters

This is the simplest form. Most programming language has this kind of parameter.

We declare required positional parameters with a type and name, e.g., int: a.

The name is for reference in the function, not at the call site.

You specify only the value without a parameter name when calling a function.

int sum(int a, int b) {
// 1
return a + b;
}
// 2
sum(1, 2);

1 a and b are use within the function.
2 At the call site, arguments are reference by the position. In this case, the argument at the first position is a, the argument at the second position is b,

The term "required" in "required positional parameters" doesn't mean it has to be non-nullable.

But it means that all arguments must be presented. If it is nullable, you have to pass null explicitly.

String greeting(String? name, int numberOfTimes) {
if (name != null) {
return 'Hello $name' * numberOfTimes;
} else {
return 'Hi' * numberOfTimes;
}
}

// 1
greeting('Sarunw', 3)
// 2
greeting(null, 3)
// 3
greeting(3);
// 2 positional argument(s) expected, but 1 found.

1, 2 You have to pass all the required positional parameters.
3 Failing to do so would result in an error.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Dart Named Parameters

We define named parameters by listing them between curly brackets {}, e.g., {int a, int b, …}.

Named parameters are optional unless they’re explicitly marked as required.

When calling a function, you can specify named arguments using paramName: value.

Here is the same sum function, but this time, we write it with named parameters.

// 1
int namedSum({required int a, required int b}) {
return a + b;
}

namedSum(a: 1, b: 1);

1 Notice that parameters are wrapped inside curly brackets {}.

We need to mark parameters as required fields since we need both of them in this function.

Here is another example where we convert the greeting function to use named parameters.

String namedGreeting({String? name, required int numberOfTimes}) {
if (name != null) {
return 'Hello $name' * numberOfTimes;
} else {
return 'Hi' * numberOfTimes;
}
}

namedGreeting(name: 'Sarunw', numberOfTimes: 3);
// 1
namedGreeting(numberOfTimes: 3);

1 Since the name parameter isn't marked as required, we can omit it when calling the function.

We can use the function with and without the parameter name since it doesn't mark as required.

And the good thing about a named parameter is the position of the argument doesn't matter (since we reference them by their name, not position).

You can put an argument wherever you want.

namedGreeting(name: 'Sarunw', numberOfTimes: 3);
namedGreeting(numberOfTimes: 3, name: 'Sarunw');

Dart Optional positional parameters

Wrapping a set of function parameters in [] marks them as optional positional parameters.

All parameters inside [] are optional. That means they must be nullable.

Here is an example of function with required positional parameter, numberOfTimes, mixed with optional positional parameters, name1 to name3.

void optionalThreeGreeting(int numberOfTimes,
[String? name1, String? name2, String? name3]) {
if (name1 != null) {
print('Hi $name1' * numberOfTimes);
}

if (name2 != null) {
print('Hi $name2' * numberOfTimes);
}

if (name3 != null) {
print('Hi $name3' * numberOfTimes);
}
}

When calling the above function, you can omit positions 2 to 4 (name1 to name3).

optionalThreeGreeting(1);
// Hi
optionalThreeGreeting(1, 'Sarunw');
// 1. Hi Sarunw
optionalThreeGreeting(2, 'Sarunw', 'Alice');
// 1. Hi Sarunw1. Hi Sarunw
// 2. Hi Alice2. Hi Alice
optionalThreeGreeting(3, 'Sarunw', 'Alice', 'Bob');
// 1. Hi Sarunw1. Hi Sarunw1. Hi Sarunw
// 2. Hi Alice2. Hi Alice2. Hi Alice
// 3. Hi Bob3. Hi Bob3. Hi Bob

How to declare parameters in Dart

These three types of Dart parameters can be used together (with restriction).

Now that we know how to declare each one of them, let's see how we can put them together.

Here are the rules:

  1. A function can have any number (zero or more) of required positional parameters.
  2. Then it can be followed either by named parameters or by optional positional parameters (but not both).

Here are some possible scenarios:

  • A function with required positional parameters.

    • Followed by named parameters.
    String greeting(String name, {String? message}) { ... }
    • Followed by optional positional parameters.
    void hide(bool hidden, [bool? animated]) { ... }
    • You can't mixed named parameters and optional positional parameters.
    // ❌
    void hide(Widget content, {required bool hidden}, [bool? animated]) { ... }
  • A function without required positional parameters.

    • Named parameters only.
    int namedSum({required int a, required int b}) { ... }
    • Optional positional parameters only.
    int namedSum({required int a, required int b}) { ... }
    • You can't mixed named parameters and optional positional parameters.
    // ❌
    int mixedSum({required int a, int? b}, [int? c]) { ... }

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Summary: Three types of Dart Parameters

Required positional parameters

  • You have to specify all arguments.
  • When calling a function, you specify each argument without their name, e.g., sum(1, 2).
  • Order of argument does matter.

Named Parameters

  • Define by putting your parameters between curly brackets {}, e.g., {required int a, int? b, …}.
  • Named parameters are optional unless they’re explicitly marked as required.
  • When calling a function, you specify argument with their name using paramName: value, e.g., sum(a: 1, b: 2).
  • You can omit non-required field at the call site.
  • Order of argument doesn't matter.

Optional positional parameters

  • Define by put your nullable parameters between [], e.g., [int? a, int? b, …].
  • All parameters inside [] are optional, which means they must be nullable.
  • When calling a function, you specify each argument without their name, e.g., sum(1, 2).
  • You can omit any arguments.
  • Order of argument does matter.

Read more article about Flutter, Dart, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
7 Xcode shortcuts for a large iOS project

This article will share seven Xcode shortcuts that will help you on a large iOS project. The tricks you are about to learn will help you find and navigate to a file/class/symbol in your large and small project.

Next
Dart initializing formal parameters

Dart has a special syntax for a constructor to initialize all instance variables. It is convenient but also confusing for a newcomer. Let's learn what it is and how to use it.

← Home