home / skills / rodydavis / skills / dart_function-invoking

dart_function-invoking skill

/skills/dart_function-invoking

This skill explains diverse ways to invoke Dart functions, including mixed positional and named args, the .call operator, and Function.apply for dynamic calls.

npx playbooks add skill rodydavis/skills --skill dart_function-invoking

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.4 KB
---
name: various-ways-to-invoke-functions-in-dart
description: Discover the surprising flexibility of calling Dart functions, including mixed positional and named arguments, the `.call` operator, and dynamic invocation with `Function.apply`.
metadata:
  url: https://rodydavis.com/posts/dart/function-invoking
  last_modified: Tue, 03 Feb 2026 20:04:32 GMT
---

# Various Ways to Invoke Functions in Dart


There are multiple ways to call a [Function](https://dart.dev/language/functions) in Dart.

The examples below will assume the following function:

```
void myFunction(int a, int b, {int? c, int? d}) {
  print((a, b, c, d));
}
```

But recently I learned that you can call a functions positional arguments in any order mixed with the named arguments. 🤯

```
myFunction(1, 2, c: 3, d: 4);
myFunction(1, c: 3, d: 4, 2);
myFunction(c: 3, d: 4, 1, 2);
myFunction(c: 3, 1, 2, d: 4);
```

In addition you can use the [`.call`](https://dart.dev/language/callable-objects) operator to invoke the function if you have a reference to it:

```
myFunction.call(1, 2, c: 3, d: 4);
```

You can also use [`Function.apply`](https://api.flutter.dev/flutter/dart-core/Function/apply.html) to dynamically invoke a function with a reference but it should be noted that it will effect js dart complication size and performance:

```
Function.apply(myFunction, [1, 2], {#c: 3, #d: 4});
```

All of these methods print the following:

```
(1, 2, 3, 4)
```

## Demo

Overview

This skill explains multiple ways to invoke functions in Dart, highlighting positional and named argument flexibility, the .call operator, and dynamic invocation with Function.apply. It shows how arguments can be mixed in surprising orders and summarizes trade-offs like code size and performance when using dynamic calls. The goal is practical guidance so you can choose the right invocation style for readability, type safety, and runtime needs.

How this skill works

The skill demonstrates direct invocation using positional and named parameters and shows that Dart allows mixing positional and named arguments in different orders. It covers calling a function reference via the .call method and performing dynamic invocation with Function.apply, which accepts a List of positional args and a Map of symbol keys for named args. It also points out that Function.apply can increase JavaScript compilation size and affect performance when targeting JS.

When to use it

  • Use standard positional + named syntax for common, type-safe calls.
  • Use the .call operator when you have a Function reference and want explicit invocation.
  • Use mixed ordering when reordering improves readability or matches calling-site ergonomics.
  • Use Function.apply for dynamic invocation when argument lists are built at runtime.
  • Avoid Function.apply in performance-sensitive or size-constrained builds (e.g., web JS).

Best practices

  • Prefer direct calls with explicit positional/named arguments for clarity and static checking.
  • Reserve Function.apply for metaprogramming, dynamic dispatch, or adapter code that truly needs runtime argument construction.
  • When using .call, keep the reference typed (e.g., void Function(int,int,{int?,int?})) to preserve static checks.
  • Document nonstandard argument ordering to avoid surprising team members and reduce bugs.
  • Test performance and bundle size impact before using Function.apply in production web builds.

Example use cases

  • Regular function calls: myFunction(1, 2, c: 3, d: 4) for clear, static usage.
  • Alternate ordering for readability: myFunction(c: 3, d: 4, 1, 2) when named context is helpful.
  • Function references: final f = myFunction; f.call(1, 2, c: 3, d: 4) in higher-order code.
  • Dynamic invocation: Function.apply(myFunction, [1, 2], {#c: 3, #d: 4}) when arguments are constructed at runtime.
  • Adapters and wrappers that receive Function objects and forward calls while preserving named arguments.

FAQ

Does mixing positional and named arguments change parameter binding?

No. Mixing order does not change which values bind to which parameters; positional arguments still fill positional parameters in left-to-right order and named arguments match by name.

When should I avoid Function.apply?

Avoid it in performance-sensitive code and web builds where it can increase JS bundle size. Prefer static calls or typed Function references when possible.