Dynamic function invocation

Gautier Siclon
Gautier Siclon

Co-founder, Apparence.io

Flutter tips · Published on

What is Dynamic Function Invocation?

In Dart, you can call functions dynamically even if you don't know their arguments or named parameters until your app is running. This is done using Function.apply.

How Function.apply Works

Function.apply lets you pass a function along with its arguments as a list and its named parameters as a map. It's super handy for situations where the function to be called, or its exact parameters, might change dynamically.

Code Example

Let's look at an example. Imagine you have a function like operatorXpBonus that calculates a bonus based on a task and an optional date:

int operatorXpBonus(Task task, {DateTime? date}) {
  return 1;
}

You can call this operatorXpBonus function dynamically like this:

void test(Task task, DateTime date) {
  final result = Function.apply(operatorXpBonus, [task], {#date: date}) as int;
}

In this code, operatorXpBonus is the function, [task] is the list of regular arguments, and {#date: date} is the map for named arguments.

Why is This Useful?

Standard function calls are usually better for performance and type safety because the compiler knows everything beforehand. However, dynamic invocation shines in specific scenarios.

It's especially useful if you're building a chatbot that needs to call different functions within your app based on user input. The chatbot might "understand" a command and then dynamically invoke the corresponding function with the right data.

Build Faster with ApparenceKit

Want to launch your Flutter app quickly? Check out ApparenceKit! It's a Flutter template designed to help you speed up your development.

Save 3 months of work

One command. Pick your modules. Firebase or Supabase auto-configured. Start building what matters.

kickstarter for flutter apps

Frequently Asked Questions

What is dynamic function invocation in Dart?

Dynamic function invocation in Dart allows you to call a function at runtime without knowing its exact arguments or named parameters beforehand. It adds flexibility to your code.

How do you dynamically invoke a function in Dart?

You use the `Function.apply` method. It takes the function you want to call, a list of its positional arguments, and a map of its named arguments.

When is `Function.apply` particularly useful?

It's very useful when you need to create flexible systems, such as a chatbot that can call various internal app functions based on user commands or external inputs.

Are standard function calls generally preferred over dynamic invocation?

Yes, standard function calls are usually preferred for better performance and type safety, as the compiler can optimize them more effectively. Dynamic invocation is for specific advanced use cases where flexibility at runtime is key.

What is ApparenceKit?

ApparenceKit is a Flutter template designed to help developers launch their applications quickly and efficiently by providing a ready-to-use foundation.

Read more
You may also be interested in
Made by ApparenceKit logo
ApparenceKit is a flutter start kit | template generator tool by Apparence.io © 2026.
All rights reserved